C - multiple definition with makefile - c

I am trying to compile my C program using make and I've come across this problem that I can't quite understand. I have 3 files in the 'calc' folder of my project: add.c sub.c and main.c. I have my Makefile located in the root folder of my project, which has the calc folder that I mentioned in it. This is what my Makefile looks like:
CC=gcc
OBJECTS=obj/main.o obj/add.o obj/sub.o
elf/new: ${OBJECTS}
${CC} -o elf/new ${OBJECTS}
obj/main.o: calc/main.c
${CC} -c -g calc/main.c -o obj/main.o
obj/add.o: calc/add.c
${CC} -c -g calc/add.c -o obj/add.o
obj/sub.o: calc/sub.c
${CC} -c -g calc/sub.c -o obj/sub.o
clean:
rm obj/${OBJECTS} elf/new
When I type 'make' into the terminal to compile, I get an error like this:
gcc -c -g calc/add.c -o obj/add.o
gcc -c -g calc/sub.c -o obj/sub.o
gcc -o elf/new obj/main.o obj/add.o obj/sub.o
obj/add.o: In function `add':
/home/bigger/workspace/test/calc/add.c:1: multiple definition of `add'
obj/main.o:/home/bigger/workspace/test/calc/add.c:1: first defined here
obj/sub.o: In function `sub':
/home/bigger/workspace/test/calc/sub.c:1: multiple definition of `sub'
obj/main.o:/home/bigger/workspace/test/calc/sub.c:1: first defined here
collect2: error: ld returned 1 exit status
makefile:5: recipe for target 'elf/new' failed
make: *** [elf/new] Error 1
And my code are there:
bigger#linux:~/workspace/test> cat calc/add.c
int add(int a, int b){
return a+b;
}
bigger#linux:~/workspace/test> cat calc/sub.c
int sub(int a, int b) {
return a-b;
}
bigger#linux:~/workspace/test> cat calc/main.c
#include <stdio.h>
#include "add.c"
#include "sub.c"
int main(int argc, char* argv[])
{
int a = 10;
int b = 5;
printf("add: %d\nsub:%d\n", a+b, a-b);
return 0;
}

When you include it is making the functions add and sub part of your main.c, then when you make you are linking main (which already has the functions by include) to the add and sub objects which have the same function symbols. You need to include header files with function declarations rather than include function definitions. See http://www.cprogramming.com/declare_vs_define.html for a longer discussion.

Related

Compile time error while running multiple c files

#Edited
I am tested on simple two files now that are add.h and add.c.
I made a Makefile in order to compile my program. Here is my makefile.
# Make file for running the project
CC=gcc
CFLAGS= -Wall -g
LDFLAGS = -include
OBJFILES = add.o
LIB = add.h
TARGET = add
all: ${TARGET}
%.o: %.c
${CC} ${CFLAGS} -c -o $# $<
${TARGET}: ${OBJFILES}
${CC} ${CFLAGS} -o ${TARGET} ${OBJFILES}
clean:
rm -f $(OBJFILES) $(TARGET) *~
when I run
make add
I get the following error:
gcc -Wall -g -c -o add.o add.c
gcc -Wall -g -o add add.o
Undefined symbols for architecture x86_64:
"_b", referenced from:
_main in add.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [add] Error 1
here are snippets of my code
add.c
#include <stdio.h>
#include <stdlib.h>
#include "add.h"
int main(void) {
int a = 10, b=20;
add(a, b);
return 0;
}
add.h
#ifndef __ADD_H_
#define __ADD_H_
extern int a,b;
int add(a,b)
{
return a+b;
}
#endif // __ADD_H_
You are lacking a rule to actually build your object files. Right now you only have one to link them all together once they already exist. Try adding:
%.o: %.c
${CC} ${CFLAGS} -c -o $# $<
This tells make how to build object files out of source files.
You are misusing the extern keyword, losing track of your variables and making your code too complicated.
Let's try something very simple:
int main(void) {
int a = 10; b=20;
return 0;
}
This fails. The compiler complains about the statement b=20;, since it has never heard of this b. The semicolon that made this a separate statement was either a typo or a conceptual error caused by declaring extern int b elsewhere. There is no need for extern here, at least not yet.
This:
int main(void) {
int a=10, b=20;
return 0;
}
works.
Now for an add function.
int add(int a, int b)
{
return a+b;
}
int main(void) {
int a = 10, b=20;
add(a,b);
return 0;
}
Note that the a and b in add are not the same variables as the a and b in main. This is crucial; do not proceed until you understand it.
Now add a declaration of the add function:
int add(int a, int b); // <- declaration
int add(int a, int b) // <- definition
{
return a+b;
}
The declaration can be moved into a header file (add.h); the definition belongs in a source file (add.c).
Finally, I would advise you to add a line to the makefile:
add.o: add.h
Aren't you missing an #endif at the end of the last header file queue.h ?
This can be for sure an issue when compiling. Moreover, aren't you missing the main_application.h header file you are then including within the stack.h one for example?
Have a look at this: https://medium.com/#m.muizzsuddin_25037/error-ld-symbol-not-found-for-architecture-x86-64-a5e5b648ffc seems helpful here and point our attention to the header files again!..

Create a static libary and link against it

Hello beautiful people,
i'm trying to create a static libary and to compile against it.
I've allready created a small static libary and a header for it.
Header (math.h):
int add (int a, int b);
int sub (int a, int b);
add.c:
int add (int a, int b) { return a + b; }
sub.c:
int sub (int a, int b) { return a - b; }
I've created my static libary with the following commands:
gcc -c add.c
gcc -c sub.c
ar rcs libmymath.a add.o sub.o
Now my main.c
#include <stdio.h>
#include "math.h"
int main( int argc, char **argv ) {
printf("Result : %d\n", add(5,7) );
return 0;
}
I can compile it with the following command:
gcc main.c libmymath.a -o main
But if i compile it the following way, it fails.
gcc main.c -lmymath -L. -o main
It fails with the following error:
/usr/bin/ld: cannot find -lmymath collect2:
error: ld returned 1 exit status
even a change to
gcc main.c -llibmymath -L. -o main
fails and even if i include the header mymath.h to gcc
Can you help me ?
gcc understood -lmymath by libmath.so or libmath.a already. So when you add lib word in -llibmymath. This case the gcc understood your library name being liblibmymath.a. So, please replace this command
gcc main.c -llibmymath -L. -o main
by
gcc main.c -o main -L. -lmymath
It should work.

Basic makefile program: in main Error undefined reference to "power"

Header lab3p2.h
#ifndef LAB3P2_H
#define LAB3P2_H
long power(int integer1, int integer2);
#endif
Power Function: lab3p2f1.c
#include "lab3p2.h"
#include <stdio.h>
long power(int integer1, int integer2){
int i;
long ret =(long) integer1;
if(integer2 ==0)
{
ret = 1;
}else{
for( i =1 ; i < integer2; i++)
{
ret = ret * integer1;
}
}
return ret;
}
Main: lab3p2.c
#include <stdio.h>
#include "lab3p2.h"
/*Takes in integers from the command line and returns the power function result*/
int main(int argc, char **argv){
int a = atoi( *(argv+1));
int b = atoi( *(argv+2));
int c =atoi( *(argv+3));
long functionResult;
functionResult = power(b,c);
printf("The result of the power function is %ld \n", functionResult);
}
MakeFile: makefile
all: lab3p2
mkprog: lab3p2.o lab3p2f1.o
gcc lab3p2.o lab3p2f1.o -o lab3p2
lab3p2.o: lab3p2.c
gcc -ansi -pedantic -c lab3p2.c
lab3p2f1.o: lab3p2f1.c
gcc -ansi -pedantic -c lab3p2f1.c
clean:
rm -rf *.o lab3p2
Why can the main not access the function?
Is something wrong with how I am compiling?
Any help is greatly appreciated.
You're missing the rule for target lab3p2; i.e. mkprog should be lab3p2:
all: lab3p2
lab3p2: lab3p2.o lab3p2f1.o
gcc lab3p2.o lab3p2f1.o -o lab3p2
lab3p2.o: lab3p2.c
gcc -ansi -pedantic -c lab3p2.c
lab3p2f1.o: lab3p2f1.c
gcc -ansi -pedantic -c lab3p2f1.c
clean:
rm -rf *.o lab3p2
With your current Makefile, when you run make without arguments, you will get the following output:
% make
gcc -ansi -pedantic -c lab3p2.c
cc lab3p2.o -o lab3p2
lab3p2.o: In function `main':
lab3p2.c:(.text+0x6b): undefined reference to `power'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'lab3p2' failed
make: *** [lab3p2] Error 1
What happens is that make tries to satisfy the first target (i.e. all); this then requires lab3p2. As make cannot find an explicit rule to build lab3p2 it then tries an implicit one - it knows that one can build foo by linking foo.o into a program; thus it runs the command
cc lab3p2.o -o lab3p2
However, this command doesn't link in the lab3p2f1.o where the definition for power resides.
These implicit rules can be quite handy with simple projects; for example for your project, the Makefile for GNU make could be simply written as
CC = gcc
CFLAGS = -ansi -pedantic
all: lab3p2
lab3p2: lab3p2.o lab3p2f1.o
clean:
rm -rf *.o lab3p2
and make would automatically figure out how to build the .o from corresponding .c and that it should use the compiler from the CC variable, and pass arguments from the CFLAGS.

How to include helper functions in C?

There are 4 files:
helper.h //contains the signatures of functions in helper.c
helper.c //implements the signatures in helper.h
file.h //has all the includes needed to run file.h
file.c //this file includes file.h and helper.h
In file.c, I need to use the function that is defined in helper.c in my main function. However, file.c is saying that there is an undefined reference to 'func_found_in_helper.c'
Is this structure correct?
Yes, provided file.c contains
#include "helper.h"
and when building your program you link together helper.o and file.o.
You also need to ensure you compile each of the files with -c so that the compiler only compiles (and not links); do the link later with all the object files.
Here's a working example (I don't actually need a main.h but if you have one of those, #include it from main.c):
main.c
#include <stdio.h>
#include <stdlib.h>
#include "helper.h"
int
main (int argc, char **argv)
{
test ();
exit (0);
}
helper.c
#include <stdio.h>
void
test ()
{
printf ("Hello world\n");
}
helper.h
void test ();
To compile
gcc -Wall -Werror -c -o main.o main.c
gcc -Wall -Werror -c -o helper.o helper.c
To link
gcc -Wall -Werror -o test main.o helper.o
In a Makefile
test: main.o helper.o
gcc -Wall -Werror -o test main.o helper.o
%.o: %.c
gcc -c -Wall -Werror -o $# $<
clean:
rm -f *.o test
To run
$ ./test
Hello world
It's a bit difficult to tell what else might be wrong without the program; my guess is you simply forgot the -c flag to gcc, or forgot to link in helper.o.
undefined reference to 'func_found_in_helper.c'
That's a little odd, as it suggests you have tried to call the function using the '.c' extension, rather than just the function name. Maybe the '.' is just a typo in the question ?
Also a linker will flag an undefined symbol, so it may also be that you have not told the linker where to find helper.o ( the helper.c file compiled to the an object file ). The compiler will start the linker automatically. Did you compile helper.c first ?

Using Assembly global variable in C

Trying to compile this code in a.s:
section .bss
global _start
global TestVar
TestVar: RESB 4
section .text
extern main
_start:
and this code in b.c:
extern int TestVar;
void test2(int x, int y)
{
int z = TestVar;
x = z + y;
y = 1;
}
int main(int argc, char **argv) {
return 0;
}
with this makefile:
all: test
test: a.o b.o
ld -melf_i386 a.o b.o -o test
a.o: a.s
nasm -f elf a.s -o a.o
b.o: b.c
gcc -m32 -Wall -g b.c -o b.o
.PHONY: clean
clean:
rm -f *.o test
running the makefile produces:
m#m-All-Series:~/testFolder$ make
nasm -f elf a.s -o a.o
gcc -m32 -Wall -g b.c -o b.o
/tmp/ccjymll2.o: In function `test2':
/home/m/testFolder/b.c:5: undefined reference to `TestVar'
collect2: error: ld returned 1 exit status
makefile:9: recipe for target 'b.o' failed
make: *** [b.o] Error 1
What am I doing wrong?
also, main is there just because if it isn't - the compiler says there is an undefined reference to main in _start in crt1.o, main will never be called, only test2, I don't know if that matters so I included that info as well.
You must use the compiler's -c option if you want to compile a C source into an object file. Without it, gcc tries to go on linking, which is not what you want. E.g.
b.o: b.c
gcc -c -m32 -Wall -g b.c
should get you a bit further.

Resources