Include a source file in a C program - c

How can I include foo() function of foo.c in this small program (sorry for my noob question):
In my foo.h file:
/* foo.h */
#include <stdio.h>
#include <stdlib.h>
int foo(double largeur);
In foo.c:
/* foo.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int foo(double largeur)
{
printf("foo");
return 0;
}
And in main.c:
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int main(int argc, char *argv[])
{
printf("Avant...");
foo(2);
printf("Apres...");
return 0;
}
After compiling:
$ gcc -Wall -o main main.c
I get this error:
Undefined symbols: "_foo",
referenced from:
_main in ccerSyBF.o ld: symbol(s) not found collect2: ld
returned 1 exit status
Thanks for any help.

$ gcc -Wall -o main main.c foo.c
GCC doesn't know to look for foo.c if you don't tell it to :)

Creating a program in C requires two steps, compiling and linking. To just run the compiling part, use the -c option to gcc:
gcc -c main.c
This creates an object file, main.o (or main.obj on Windows). Similarly for gcc -c foo.c. You won't get the error message above at this stage. Then you link these two object files together. At this stage, the symbol foo is resolved. The reason you got the error message was because the linker couldn't find the symbol, because it was only looking at main.o and not foo.o. The linker is usually run from gcc, so to link your object files and create the final executable file main, use
gcc -o main main.o foo.o

You have to compile foo.c also because it is another module. Let me see how they do it in gcc:
$ gcc -Wall main.c foo.c -o main

You could also do this in your MakeFiles, like this:
APP_NAME = Foo
Foo_HEADERS = foo.h
Foo_FILES = main.c foo.c
If you're not so much familiar with MakeFiles i suggest you to take a look at Make Docs, but this is a simple example, APP_NAME sets the name of the compiled executable(in this case is Foo), Foo_HEADERS will set the headers used by your application, Foo_FILES you will set the source files of your applications, remember to put the APP_NAME(in this case Foo) at the beginning of _HEADERS and _FILES. I suggest you to use MakeFiles because they will organize you application build process and will be better for the end-user.

Related

Header file issue: Multiple definition error; first defined here

I am having trouble with multiple definitions of functions. All other solutions here on stack overflow have not worked out for me.
This is my main.c:
#include "lib.h"
int main(){
test();
}
This is the lib.c file:
#include "lib.h"
int var;
void test(){
//code here
}
And this is the lib.h file:
#ifndef _HTTPLIB_H_
#define _HTTPLIB_H_
#include <stdio.h>
extern int var;
extern void test();
#endif
I have checked and there are no definitions of any function twice and I am never including a .c source file.
I am compiling with
gcc lib.c main.c -Wall -g -o main
main: In function 'test': (.text+0xfdd): multiple definition of 'test' /tmp/ccb8byZi.o:lib.c:(.text+0xef9): first defined here'
real code:
main file: http://pastebin.com/xr3DF0TE
lib.c and lib.h file: http://pastebin.com/KemhKX3f
This is the compilation code
gcc -lpthread -D_REENTRANT httplib.c http.c -o -g http
real error message:
http: In function `sigusr1':(.text+0xfdd): multiple definition of `sigusr1'/tmp/ccb8byZi.o:httplib.c:(.text+0xef9): first defined here
gcc -lpthread -D_REENTRANT httplib.c http.c -o -g http
Here's your problem: You're telling gcc to compile 3 files (httplib.c, http.c, http) into an executable called -g. This is because the argument after -o is taken to be the output filename.
The errors are caused by you apparently having an http executable lying around, which already contains the (compiled) functions defined in httplib.c.
Fix:
gcc -lpthread -D_REENTRANT httplib.c http.c -g -o http

netfilter queue undefined reference to `nfq_open'

I have writting this code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <linux/types.h>
#include <linux/netfilter.h>
#include <libnetfilter_queue/libnetfilter_queue.h>
int main(int argc, char **argv)
{
struct nfq_handle *h;
printf("opening library handle\n");
h = nfq_open();
nfq_close(h);
exit(0);
}
and when I try to compile it says that:
/tmp/ccEv9MYS.o: In function `main':
test1.c:(.text+0x1a): undefined reference to `nfq_open'
test1.c:(.text+0x2a): undefined reference to `nfq_close'
collect2: ld returned 1 exit status
I tried checking if the library is found by gcc and it is (when I modifiy the incluse of libnetfilter_queue there is an error), I recompiled the library and made sur that the fonctions I'm calling are in in it.
If you have any clue thanks for helping
Icompile using this:
gcc -o test test1.c
I have also tried:
gcc -o test -lnetfilter_queue test1.c
gcc -o test -L/usr/local/lib test1.c
Well, from the gcc manual page, for the -llibrary linking option
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
That says, the linker works from left to right, so need to put the dependent on left hand side.
You need to change your compilation statement to
gcc -o test test1.c -lnetfilter_queue

Compiling using multiple headerfiles in GCC -- undefined reference to .. math

I've been working on this for a while now. I have these files:
main.c
zSim.h
zSim.c
zDynamicArray.h
zDynamicArray.c
zOptions.h
zOptions.c
zMain.h
zMain.c
All of the files and headers are located within the same folder.
My main has the following includes:
#include "zDynamicArray.h"
#include "zOptions.h"
#include "zMain.h"
#include "zSim.h"
#include <stdio.h>
#include <math.h>
I am using header guards. Just for example, this is from my zSim.h file:
#ifndef SIM_H
#define SIM_H
#include "zDynamicArray.h"
#include "zOptions.h"
//This Header is accountable
//for all Simulation Related things.
int Simulate(SIMOPT so,PRINTOPT po);
#endif
and this is a snippet from my zSim.c code (maybe I am doing something wrong here?):
#define M_PI 3.14159265358979323846
#include "zSim.h"
#include "zDynamicArray.h"
#include "zOptions.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
I am compiling using this gcc command:
gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST
and I also used:
cc -c main.c
cc -c zSim.c
cc -c ... etc
cc *.o -o TEST
They all result in an undefined reference for anything in the math.h library. Also when I use the gcc -I command, math is still undefined.
If i compile using gcc main.c, I get unresolved references for anything in my header files.
What should I do?
try ading -lm at the end : gcc -c main.c cc -c zSim.c cc -c ... etc cc *.o -o TEST -lm
You have to link the math library with the -lm option. The reason why is explained here : Why do you have to link the math library in C?
they all result in an undefined reference for anything in the math.h library
math.h is only the header file which contains prototypes and type declarations. You also need to link against the math library when creating your executable. Add -lm to your gcc call:
gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST -lm
See also Undefined reference to `sin`.

Cuda C - Linker error - undefined reference

I am having a hard time compiling a simple cuda program consiting of only two files.
The main.c looks like this:
#include "my_cuda.h"
int main(int argc, char** argv){
dummy_gpu();
}
The cuda.h looks like this:
#ifndef MY_DUMMY
#define MY_DUMMY
void dummy_gpu();
#endif
And the my_cuda.cu file loos like this:
#include <cuda_runtime.h>
#include "my_cuda.h"
__global__ void dummy_gpu_kernel(){
//do something
}
void dummy_gpu(){
dummy_gpu_kernel<<<128,128>>>();
}
However if I compile I allways receive the following error:
gcc -I/usr/local/cuda/5.0.35/include/ -c main.c
nvcc -c my_cuda.cu
gcc -L/usr/local_rwth/sw/cuda/5.0.35/lib64 -lcuda -lcudart -o md.exe main.o my_cuda.o
main.o: In function `main':
main.c:(.text+0x15): undefined reference to `dummy_gpu'
collect2: ld returned 1 exit status
Thank you for your help.
You have a problem with symbol name mangling. nvcc uses the host C++ compiler to compile host code, and this implies that symbol name mangling is applied to code emitted by the CUDA toolchain.
There are two solutions to this problem. The first is to define dummy_gpu using C linkage, so change your my_cuda.cu to something like this:
extern "C" {
#include "my_cuda.h"
}
.....
extern "C"
void dummy_gpu(){
dummy_gpu_kernel<<<128,128>>>();
}
Note that you will need to change your linkage command to this:
gcc -L/usr/local_rwth/sw/cuda/5.0.35/lib64 -o md.exe main.o my_cuda.o -lcuda -lcudart
because the CUDA shared libraries need to be specified after the object files that use them.
Your second alternative would be to use either g++ or nvcc to do the linking, in which case the whole problem should disappear.
You have a C/C++ linkage problem. nvcc is decorating things in a C++ fashion but your gcc compiler is handling things using C style linkage. A simple way to fix it is to rename your main.c to main.cpp and then repeat your commands using g++ instead of gcc

Why does CC not see my function definition in header?

I'm writing a simple application in ANSI C. I am using GCC in a Unix environment.
I have the following sample application:
//main.c
#include "foo.h"
int main()
{
int result;
result = add(1,5);
return0;
}
Header:
//foo.h
#ifndef FOO_H_INCLUDED
#define FF_H_INCLUDED
int add(int a, int b);
#endif
Implementation:
//foo.c
int add(int a, int b)
{
return a+b;
}
I am compiling my program with the following command:
cc main.c -o main.o
The compiler complains that 'reference to add is undefined'. Is this a linking problem? How do properly make use of my header?
Thanks!
You need to compile both your source files together:
cc main.c foo.c -o main
Also, in this case, -o produces an executable, so calling it main.o can be misleading.
Yet another tidbit, though unrelated to the question: the #ifndef and #define in foo.h don't match.
The header is not your current problem. Your current problem is that you're not compiling the add function definition in foo.c.
Try
cc main.c foo.c -o main.o
If you are trying to compile main.c into an assembled object file, you need to prevent gcc from trying to link. This is done via
cc -c main.c -o main.o
You can compile all other object files, then when you have all of your object files ready, you simply do
cc main.o obj1.o anotherOBJ.o -o myExecutableBinary
"undefined reference" is a linker error, not a compiler error.
The compiler sees the declaration in the header, but you have not compiled or linked the definition in foo.c. Your title uses the term definition incorrectly.

Resources