2

I am trying to compile my C program and I am getting some weird compiling errors and I have no idea where it is coming from. I already found similar posts, but their solution of specifying the output with -o is not working.

SO this is how my makefile looks like (shortened up):

CC = gcc -O3 -Wextra -Wall -pg -g -std=c99

OBJ = ./src/main.o ./src/FUNC.o ./src/getRoot.o ./src/getTree.o 

out: $(OBJ)
    g++ -std=c99 -g -o ./myProgramm $(OBJ)


./src/FUNC.o: src/FUNC.c
    $(CC) -c src/FUNC.c -o ./src/FUNC.o

./src/main.o: src/main.c
    $(CC) -c src/main.c -o ./src/main.o

./src/getRoot.o: src/getRoot.c
    $(CC)   -c src/getRoot.c -o ./src/getRoot.o

./src/getTree.o: src/getTree.c
    $(CC)   -c src/getTree.c -o ./src/getTree.o

This is a part of the errors i am getting:

./src/FUNC.o:(.rodata+0x78): multiple definition of `khStrInt'
./src/main.o:(.rodata+0x0): first defined here
./src/FUNC.o: In function `get_nbr_edge_kmer':
 /home/Documents/EXAMPLE_CODE/src/FUNC.c:126: multiple definition of `DISTANCE_MAX'
./src/main.o:(.rodata+0x4): first defined here
./src/getRoot.o:(.rodata+0x0): multiple definition of `DISTANCE_MAX'
./src/main.o:(.rodata+0x4): first defined here
 ./src/main.o:(.rodata+0x4): first defined here
 ./src/getTree.o:(.rodata+0x0): multiple definition of `DISTANCE_MAX'
 ./src/main.o:(.rodata+0x4): first defined here
 ./src/getRoot.o:(.rodata+0x0): multiple definition of `khStrInt'

Does someone maybe have some idea what i am doing wrong here :/

10
  • Having source code would be useful, you might have the function khStrInt defined in a header file (defined means it has a body) and have the header file included in multiple .c files. This means the compile generates the code for khStrInt multiple times and so you get your multiple definition errors. A similar thing applies for your other multiple definitions. You can solve this by only declaring the functions in the header and defining the function body in one .c file. Commented Feb 5, 2017 at 20:33
  • Unforunately, this is not the problem ... khStrInt is only a constant const int khStrInt = 33 defined in getRoot.h and then the other files are including this header file to use it. Commented Feb 5, 2017 at 20:40
  • And yes, I am structuring the code as you have suggested, so writing the function prototypes in the header and then defining the function bodies in the c files, and then including the headers in the C files where i need them Commented Feb 5, 2017 at 20:41
  • @malajedala That's not a constant, that's a (read-only) variable. So every C file that includes this header contains a definition of that variable. Commented Feb 5, 2017 at 20:47
  • @malajedala const int VarNameHere = ...; is both a declaration and a definition. Being in a header file means any source files that include it will get both, and therefore you have multiple declarations (ok) and definitions (not ok) of identical identifiers across multiple translation units. Linking those compiled units is where the problem finally surfaces (this is not a compile-time error; it's a link-time error). Commented Feb 5, 2017 at 20:51

2 Answers 2

2

Inside your header file, you should declare your variable like:

extern const int khStrInt;

Then in a .c file, you should define it like:

const int khStrInt = 33;

This means the variable definition is only generated once by the compiler when compiling the .c file and so the linker doesn't see multiple definitions. Also, having the declaration in the header file allows other files which include the header to be able to use the variable.

Sign up to request clarification or add additional context in comments.

5 Comments

@melpomene Sorry for making that mistake, thanks for the edit.
interesting that you mention that.. I just managed to reduce the problem, so I am using some C library i have from github, and now the only mistakes I am getting are from these ones! SO i get an error like lib.h "undefined reference to rev". And then I checked the header and C files, and it looked like you just proposed as a solution. so having extern const int rev; in that header and const int rev = 100; in the lib.c file. So isnt this than wrong if my compiler complains exactly here?
@malajedala I think you need to compile the library that you are using from github as well and also link it with your program.
@malajedala That's fine but you might need to compile the library you are using. Which github library are you using?
Thank you so much Winestone! I just compiled my code and the library,and replaced in my code the varible declaration as you explained and now it is compiling just fine! Thank you
-1

Quite likely the problem is caused by lack of #include guards.

To prevent a file from being included more than once

#ifndef myheader_h
#define myheader_h

#define DISTANCE_MAX 1000

#endif

1 Comment

That makes no sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.