I have tried looking through other answers, unfortunately I have not found anyone with quite the same problem. I am trying to link two C files together, one has a header file, and the other the main file. When trying to make I receive this error:
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Below is the makefile I use to compile.
INC_DIR = include
SRC_DIR = src
BIN_DIR = bin
UNAME := $(shell uname)
CC = gcc
CFLAGS = -Wall -std=c11 -g -I$(INC_DIR) -I/usr/include/libxml2/
LDFLAGS= -L.
all: XMLParser
XMLParser: $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
$(CC) $(CFLAGS) -o XMLParser $(BIN_DIR)/XMLParser.o $(BIN_DIR)/SVGParser.o
$(BIN_DIR)/XMLParser.o: $(SRC_DIR)/XMLParser.c $(INC_DIR)/SVGParser.h
$(CC) $(CFLAGS) -o $(BIN_DIR)/XMLParser.o -c $(SRC_DIR)/XMLParser.c
$(BIN_DIR)/SVGParser.o: $(SRC_DIR)/SVGParser.c $(INC_DIR)/SVGParser.h
$(CC) $(CFLAGS) `xml2-config --cflags --libs` -o $(BIN_DIR)/SVGParser.o $(SRC_DIR)/SVGParser.c
clean:
rm *.o XMLParser $(BIN_DIR)/*
XMLParser.c has my main function:
int main(int argc, char **argv) {
...;
return 0;
}
Your recipe for generating the intermediate SVGParser.o is attempting to link and generate a runnable executable. You need to add -c to skip the link step.
You will also need to remove the —libs on that object file and move it to the final link step. In other words add xml2-config —libs for the rule generating the final XMLParser target.
Disclaimer: I’ve never specifically used xml2-config, so I guess there’s some chance I’m wrong here, but it’s a very common paradigm for building and linking against complex libraries, I assume it’s following the same patterns.
A full sample code showing the error must be posted to be able to test it elsewhere, so I cannot fully test your makefile, but providing a true main() and see what happens.
After generating stub files to check your Makefile I found the following output from make:
$ make
gcc -Wall -std=c11 -g -Iinclude -I/usr/include/libxml2/ -o bin/XMLParser.o -c src/XMLParser.c
gcc -Wall -std=c11 -g -Iinclude -I/usr/include/libxml2/ `xml2-config --cflags --libs` -o bin/SVGParser.o src/SVGParser.c
/usr/local/bin/ld: /usr/lib/gcc/x86_64-unknown-freebsd13.0/10.2.1/../../../crt1.o: en la función `_start':
/home/usr/src/lib/csu/amd64/crt1_c.c:75: referencia a `main' sin definir
collect2: error: ld devolvió el estado de salida 1
*** Error code 1
Stop.
make: stopped in /home/lcu/so/makefile
$ _
which is trying to link (alone) the module SVGParser.o (THIS IS BECAUSE YOU DIDN'T ADD THE -c COMPILATION OPTION TO COMPILE ONLY).
Change the compilation line for the SVGParser.o module to use the -c compile-only option and you will get the executable properly.
Related
I want to use the ta_lib functions in my C code and am trying to import the ta_lib functions. The header file gets imported correctly but i cannot get the linker to find the actual library.
I want to do the compiling process with MAKE and gcc.
Firstly I import the header
#include <ta-lib/ta_libc.h>
And then when i need to use a function
TA_ADOSC(0, CSV_LENGTH - 1, temp_high, temp_low, temp_close, temp_volume, 3, 10, &beginIdx, &endIdx, tmp_adosc);
The program compiles fine using my makefile
# create CC variable
CC = gcc
# create CFLAGS variable
CFLAGS = -L/usr/local/lib -Wall -g
LDLIBS = -lta_lib -I/usr/local/include -lm
output: main.o
$(CC) $(CFLAGS) -o output main.o
main.o: main.c
$(CC) $(LDLIBS) -c main.c
# target: dependencies
# action
clean:
rm -f \*.o output
Once I try to run make i get the following
gcc -L/usr/local/lib -Wall -g -o output main.o
/usr/bin/ld: main.o: in function `calculate_indicators': main.c:(.text+0x226): undefined reference to `TA_ADOSC'
collect2: error: ld returned 1 exit status
make: \*\*\* \[Makefile:10: output\] Error 1
From my understanding I need to fix the linking to the shared library.
The library is installed:
ldconfig -p | grep libta_lib.so
Returns the following
libta_lib.so.0 (libc6,x86-64) => /usr/local/lib/libta_lib.so.0
libta_lib.so.0 (libc6,x86-64) => /lib/libta_lib.so.0
libta_lib.so (libc6,x86-64) => /usr/local/lib/libta_lib.so
libta_lib.so (libc6,x86-64) => /lib/libta_lib.so
Since i am fairly new to C and using external libraries I can't find what seems to be the problem
You are adding the libraries to the compile line. They need to be added to the link line. And preprocessor options like -I are used by the compiler, and "where to find libraries" options like -L are used by the linker.
Also, libraries always must come at the end of the link line, after all the object files. And, the -L "where to search" option should come before the -l "what library to find" option.
Write your rules like this:
CFLAGS = -I/usr/local/include -Wall -g
LDFLAGS = -L/usr/local/lib
LDLIBS = -lta_lib -lm
output: main.o
$(CC) $(CFLAGS) $(LDFLAGS) -o output main.o $(LDLIBS)
main.o: main.c
$(CC) $(CFLAGS) -c main.c
However, it's better to just let make do the work for you; it knows how to correctly compile things (as long as you set the standard variables). You don't need to include a rule to build main.o at all.
I need to make a makefile for my project and I have two interdependent libraries which are libpcsc.so and libccid.so .
could someone tell me what my mistake is? Thanks for your answers in advance.
and please let me know when you need more information.
my makefile is like:
by the way, one of lib directory is /home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/ and other one's is usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/
CC = gcc
LIB_DIRS = -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/
-L../../usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/
INCLUDE_DIR = /home/hasanbucak/Desktop/ccid-1.4.28/src/ #ccid_usb.h
CFLAGS = -g -Wall
obj-y:= smcard
default: all
all: smcard
smcard:
$(CC) $(CFLAGS) $(LIB_DIRS) -I$(INCLUDE_DIR) -c -o $(obj-y).o $(obj-y).c
$(CC) $(CFLAGS) -lccid -lpcsclite $(LIB_DIRS) -I$(INCLUDE_DIR) -o $(obj-y) $(obj-y).c
clean:
rm $(obj-y).o $(obj-y)
and when I write make in terminal , it says:
hasanbucak#ubuntu:~/Desktop/hasan$ make
gcc -g -Wall -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/ -L../../usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/ -I/home/hasanbucak/Desktop/ccid-1.4.28/src/ -c -o smcard.o smcard.c
gcc -g -Wall -lccid -lpcsclite -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/ -L../../usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/ -I/home/hasanbucak/Desktop/ccid-1.4.28/src/ -o smcard smcard.c
/usr/bin/ld: cannot find -lccid
collect2: error: ld returned 1 exit status
Makefile:20: recipe for target 'smcard' failed
make: *** [smcard] Error 1
Make already told you what was wrong with the Makefile:
/usr/bin/ld: cannot find -lccid
You should specify the path to ccid properly:
LIB_DIRS = -L/home/hasanbucak/Desktop/pcsc-lite-1.8.23/src/.libs/
-L/usr/lib/pcsc/drivers/ifd-ccid.bundle/Contents/Linux/
Note that /usr and ../../usr are not at all the same directory.
The first one is absolute (i.e. relative to your root directory), while the second is relative, it expands to ~/Desktop/hasan/../../usr, which is equivalent to /home/hasanbucak/usr.
I'm new to C programming, and I'm trying to compile this Simple training example with GCC on Ubuntu 12.10.
Looks like fann.h should not be included (as stated on the file itself), so I included fixedfann.h instead.
First attempt (without include, just to see what the compiler will ask for):
$ gcc main.c -o output
/tmp/cckKyM92.o: In function `main':
main.c:(.text+0x62): undefined reference to `fann_create_standard'
main.c:(.text+0x7a): undefined reference to `fann_set_activation_function_hidden'
main.c:(.text+0x8e): undefined reference to `fann_set_activation_function_output'
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
main.c:(.text+0xce): undefined reference to `fann_save'
main.c:(.text+0xda): undefined reference to `fann_destroy'
collect2: ld returned 1 exit status
fann_create_standard is on fann.h and fann.c. As fann.h is included by fixedfann.h, and fann.h should not be included directly, I believe I have to compile fann.c and fixedfann.c, and link then (tell me if I'm doing any mistake, I'm still not familiar with this "linking" stuff).
So I did:
$ gcc fann/fixedfann.c -o fann/fixedfann.o
fann/fixedfann.c:22:20: fatal error: config.h: No such file or directory
compilation terminated.
and then I did:
$ gcc fann/fixedfann.c -o fann/fixedfann.o -include fann/include/config.h
fann/fixedfann.c:22:20: fatal error: config.h: No such file or directory
compilation terminated.
Now, why it's not finding the config.h file here?
--update
Thanks #JonathanLeffler, I could make some steps here. But now I'm stuck at:
$ gcc fann/fixedfann.c -o fann/fixedfann.o -I./fann/include/ -lm
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
and, with grep, I could not find any reference to main on the fann folder... Also no function _start, and I don't know who is linking this crt1.o... Any idea what's wrong here?
--update2
Ok, I got the .o files using Harmeet's Makefile, now I'm trying to link everything.
I created the main.o with gcc -c main.c, and I tried:
gcc -o output main.o fann/fixedfann.o -lm
(-lm for the libmath, that is needed) and I got:
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
This fann_train_on_file is on fann_train_data.c, so I tried:
gcc -o output main.o fann/fixedfann.o fann/fann_train_data.o -lm
but I got lots of multiple definition of... errors... :/
Looks like fann_train_data.o is already included/linked, but if so, why it's not finding fann_train_on_file?
--update3
I'm still really stuck here... Any idea of which (if any) of this two lines should work?:
gcc -o output main.o hello.o fann/fixedfann.o fann/fann_train_data.o -lm
or
gcc -o output main.o hello.o fann/fixedfann.o -lm
--update for Harmeet
The output was:
$ make
gcc -L./fann -lfann main.o -o main
main.o: In function `main':
main.c:(.text+0x62): undefined reference to `fann_create_standard'
main.c:(.text+0x7a): undefined reference to `fann_set_activation_function_hidden'
main.c:(.text+0x8e): undefined reference to `fann_set_activation_function_output'
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
main.c:(.text+0xce): undefined reference to `fann_save'
main.c:(.text+0xda): undefined reference to `fann_destroy'
collect2: ld returned 1 exit status
make: *** [main] Error 1
You can use ar to make a static library and work with that.
Create a Makefile under your hello-fann-3/fann/ folder with the following contents -
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
CFLAGS = -c -Iinclude
all: libfann.a
libfann.a: $(OBJECTS)
ar rcs $# $^
%.o: %.c
gcc $(CFLAGS) $^
Then use the make command in hello-fann-3/fann/ to build the static library. The above Makefile will generate libfann.a that you can link to your program.
Create a Makefile under your hello-fann-3/ folder with the following contents -
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
CFLAGS = -c -I./fann/include
LFLAGS = -L./fann -lfann
main: $(OBJECTS)
gcc $(LFLAGS) $^ -o $#
%.o: %.c
gcc $(CFLAGS) $^
Then use the make command in hello-fann-3/ to build the main program.
In your main.c, you must include fan.h like -
#include "fann.h"
If you do not understand the Makefile, you can read about it here -
http://www.gnu.org/software/make/manual/html_node/index.html
You just need to link the fann library.
If you compile manually do this
gcc main.c -lfann -lm -o main
then simply run it like
./main
If you are on Ubuntu and you faced the following error
./main: error while loading shared libraries: libfann.so.2: cannot open shared object file: No such file or directory
Then run
sudo ldconfig
If you are using NetBeans, then simply Right click on your project -> Properties -> Build -> Linker,
then in the Libraries section click on the browse button [...] then in the new window click on Add Library...
Then add fann library (for example my fann library path is: /usr/local/lib/libfann.a) and click Ok
A fellow helped me, and we came to this line that compiled everything, and make the executable:
$ gcc fann/fann.c fann/fann_io.c fann/fann_train.c fann/fann_train_data.c fann/fann_error.c fann/fann_cascade.c main.c -Ifann/include -lm
And this is the answer.
That said, this is exactly what fixedfann.c is doing (include all this .c files). But if I try:
$ gcc fann/fixedfann.c main.c -Ifann/include -lm
..I get:
undefined reference to `fann_train_on_file'
This fann_train_on_file is on fann_train_data.c, which is included by fixedfann.c, so why it is undefined? I don't know... :/
--update
I realized that:
$ gcc fann/fixedfann.c main.c -Ifann/include -lm
will work if I comment the headers on fixedfann.c:
//#include "config.h"
//#include "fixedfann.h"
#include "fann.c"
#include "fann_io.c"
#include "fann_train.c"
#include "fann_train_data.c"
#include "fann_error.c"
#include "fann_cascade.c"
DESCRIPTION:
I have a library libshell.a, inside of it is the function ord_interna that i'm attempting to use, however it seems i linked it wrong, could you guys fix my error, so i dont make it in the future? Cheers,
Error:
/tmp/ccn5lbmJ.o: In function `main':
minishell.c:(.text+0x4e): undefined reference to `ord_interna'
collect2: error: ld returned 1 exit status
make: *** [minishell.o] Error 1
Makefile:
CC=gcc
CFLAGS=-Wall -pedantic -c
all: microshell
microshell: minishell.o
gcc minishell.o -o microshell
minishell.o: minishell.c
gcc minishell.c minishell.h entrada_minishell.c entrada_minishell.h ejecutar.c ejecutar.h libshell.a
clean:
rm -rf *o microshell
From your makefile, I'm guessing you have these source files:
minishell.c
entrada_minishell.c
ejecutar.c
And that you want to compile them, and then link them all together with libshell.a to create an executable called microshell. In that case, you want something like:
CC=gcc
CFLAGS=-Wall -pedantic
all: microshell
microshell: minishell.o entrada_minishell.o ejecutar.o
$(CC) -o $# $^ -L. -lshell
You can add a clean target if you want, but just that should get you going.
Editorial notes:
it's really weird to put header files on the compilation line; I assumed you didn't actually want to do that.
You should look into gcc's -MMD flag to do automatic dependency generation.
I was auditing cs107 at stanford online
The problem I ran into is with assignment 6, when I type "make" in terminal, the error message pops up. Basically, I miss two header files, which I guess can be got from the pre-compiled .lib file. But somehow it just doesn't work.
Here's part of the original make file:
CFLAGS = -D_REENTRANT -g -Wall -D__ostype_is_$(OSTYPE)__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function $(DFLAG)
LDFLAGS = -L/usr/class/cs107/assignments/assn-6-rss-news-search-lib/$(OSTYPE) -L/usr/class/cs107/lib -lexpat -lrssnews $(PLATFORM_LIBS) $(THREAD_LIBS)
PFLAGS= -linker=/usr/pubsw/bin/ld -best-effort -threads=yes -max-threads=1000
Edit:
When I said "This is supposed to compile even without threading implementation", I meant that it should compile without FURTHER threading implementation by students.
So here's the error message with thread:
gcc -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -c -o rss-news-search.o rss-news-search.c
rss-news-search.c: In function ‘main’:
rss-news-search.c:109:3: warning: implicit declaration of function ‘InitThreadPackage’ [-Wimplicit-function-declaration]
gcc rss-news-search.o -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -L/home/h/cs107/assn-6-rss-news-search-lib/linux -L/usr/class/cs107/lib -L. -lexpat -lrssnews -lnsl -lpthread -lthread_107_linux -o rss-news-search
/usr/bin/ld: cannot find -lthread_107_linux
collect2: ld returned 1 exit status
make: *** [rss-news-search] Error 1
here's the error message without $(THREAD_LIBS):
gcc -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -c -o rss-news-search.o rss-news-search.c
rss-news-search.c: In function ‘main’:
rss-news-search.c:109:3: warning: implicit declaration of function ‘InitThreadPackage’ [-Wimplicit-function-declaration]
gcc rss-news-search.o -D_REENTRANT -g -Wall -D__ostype_is_linux__ -std=gnu99 -I/usr/class/cs107/include/ -Wno-unused-function -L/home/h/cs107/assn-6-rss-news-search-lib/linux -L/usr/class/cs107/lib -L. -lexpat -lrssnews -lnsl -lpthread -o rss-news-search
rss-news-search.o: In function `main':
/home/h/cs107/assn-6-rss-news-search/rss-news-search.c:109: undefined reference to `InitThreadPackage'
collect2: ld returned 1 exit status
make: *** [rss-news-search] Error 1
In the later case, if I comment out "InitThreadPackage", it compiles just fine.
This is the procedure to compile your project:
Create a file assn-6-rss-news-search/thread_107.h, and put this inside:
/* Empty header file */
Copy the library librssnews.a from assn-6-rss-news-search-lib/linux/ to assn-6-rss-news-search/
Modify the file rss-news-search.c by commenting the call to the function : InitThreadPackage on line 109:
//InitThreadPackage(false);
Modify the Makefile to include the path to the current directory (to be able to link to the library you've copied earlier librssnews.a):
The line 27 should look like this:
LDFLAGS = -L/usr/class/cs107/assignments/assn-6-rss-news-search-lib/$(OSTYPE) -L/usr/class/cs107/lib -L. -lexpat -lrssnews $(PLATFORM_LIBS) $(THREAD_LIBS)
Then:
make clean
make
EDIT :
When you got this error cannot find lthread_107_linux, Edit your Makefile to remove this $(THREAD_LIBS) on line 27:
LDFLAGS = -L/usr/class/cs107/assignments/assn-6-rss-news-search-lib/$(OSTYPE) -L/usr/class/cs107/lib -L. -lexpat -lrssnews $(PLATFORM_LIBS)
The class-specific header files, like thread_107.h are found in /usr/class/cs107/include/ on whatever machine the instructor is expecting the students to use. If you're not using that machine, you'll have to copy those include files or make your own.
The expat.h file is from an open source library. You'll need to install the appropriate package on the system you're compiling on. On Ubuntu, that's sudo apt-get install libexpat1-dev, but the package name should be similar on other distributions.