Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declaration in C and C++ and the habit of arranging code

Tags:

c++

c

I don't know what this problem belong to , Just please spend your time to read. It involves a difference about C and C++ and a habit of writing code; the code is as follows: I divide it into 3 files; the main.c

   #include"myh.h"
    unit_t *paa;
    int main()
    {
       paa=(unit_t*)malloc(sizeof(unit_t));
       if(paa==NULL){
         printf("out of memory\n");
         exit(1);
       } 

       fuzhi(paa);
       printf("hello !%d",paa->number);
       free(paa->msg);
      free(paa);
       paa=NULL; 
       return 0;

    }  

anohter c: ke.c

#include"myh.h"
void fuzhi(unit_t* pa)
{
   pa->number=3;
   pa->msg=(char *)malloc(20);
   printf("fuzhi !");

}

the h file: myh.h

#ifndef P_H
#define P_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef  struct{
      int number;
      char *msg;
}unit_t;
void fuzhi(unit_t* pa);
int a; 

#endif

So the problem is when I run the code using C it has no problem ,but when I save it as cpp, the error is multiple definiton of 'a'; why? The second question is I don't know the habit I arrange the code is good or not. Is someone give me some good advice? When the code is big , I usually put the declaration in the h file and use a c/cpp the write the definition of the function. Then use a main c/cpp to meet the main function. Can someone give me some good advice about writing code, I am a new learner. Thanks.

like image 915
Fanl Avatar asked Dec 03 '25 11:12

Fanl


2 Answers

The first thing you have to remember is that C and C++ are different languages, and so have different rules about globally declared variables. And you do have the global variable a defined multiple times: Once in the main.c file and once in the ke.c file.

This is because you define the variable in the header file, which you include in both your source files. What you should do is declare the variable in the header file, and then define it in a single source file.

For example, in the header file you should have

extern int a;

And in one source file:

int a;
like image 95
Some programmer dude Avatar answered Dec 06 '25 00:12

Some programmer dude


The compilation process as follows:

For each c. or .cpp file (called compilands):

  • preprocess the file, resolving all macros, #includes, etc...
  • compile the file, producing the object file .o. Object file contains machine code, but non-local variables are still referenced by names.

Then at the end link all object files, which includes:

  • resolving all the non-local variable names
  • replacing those with addresses

In your case, you have two compilands: main.c and ke.c.

During preprocessing, both include the .h file. As a result, both compilands declare a global variable int a. Each compiland uses its own copy of that global variable.

Then, the linker tries to resolve the names and is surprised seeing two object files defining two variables under the same name.


To solve it, in C++ header you use the keyword "extern":

extern int a;

This indicates that there will be a global variable a, but it not defines it (similarly to a function declaration without definition). In one of the compilands you then need to repeat the normal int a definition, but in a place that is not seen by other compilands (that is - not in a header)

like image 37
CygnusX1 Avatar answered Dec 06 '25 00:12

CygnusX1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!