gcc Link Order issue - c

I am currently having problems compiling with the following line:
gcc test.c -I/usr/include -L/lib -lipc -lpcd -lrt -o /home/examples/bin/test
I was suggested to group them using start-group and end-group.
I am not able to get the proper syntax.
I think i need this part, but what do the whole line look like?
-Wl,--start-group -lipc -lpcd -lrt -Wl,--end-group

Which problem are you having ?
Anyway, try putting the linker arguments at the end:
gcc test.c -o /home/examples/bin/test -I/usr/include -L/lib -lipc -lpcd -lrt

Related

gcc cannot find -lglfw3

I'm on a linux system (arch linux specifically) and I'm trying to compile the introduction project from the official glfw page but I cannot seem to get gcc to compile it. If anyone doesn't know what I'm talking about it's this.
Also this is how I'm trying to compile it:
gcc -Iinclude test.c -o test -lglfw3 -lm -lGL -lGLU
and it gives me the following errors:
/usr/bin/ld: cannot find -lglfw3
collect2: error: ld returned 1 exit status
I completely forgot about this question until I got a notification for it. For me the solution was to not use -lglfw3 but rather use -lglfw
If you've installed pkg-config, and glfw3.pc is in the search path, try:
gcc -Iinclude test.c -o test `pkg-config --libs glfw3` -lm -lGL -lGLU
If you only have the static build, use: pkg-config --static --libs glfw3, which will add the dependencies that libglfw3.a requires.
Find libglfw3.a or libglfw3.so on your system
Mention that path to gcc using -L
gcc -Iinclude test.c -o test -L/dir/where/glfw3/resides -lglfw3 -lm -lGL -lGLU

How to compile two C programs?

This is my makefile:
all: prgrm1 prgrm2
prgrm1: prgrm1.c
gcc -o prgrm1 prgrm1.c -lrt
prgrm2: prgrm2.c
gcc -o prgrm2 prgrm2.c -lrt
When I try to compile I get the message "Nothing to be done for 'all'."
I made sure that I used tabs not spaces so that is not it. What am I doing wrong?

.so: undefined reference to 'min'

My c application linked .so library. But application says
library.so: undefined reference to 'min'
My compiler command like:
gcc -o test.o library.so -ldl -lpthread -lm
Is there any solution? Please help me
You need to pass -library to gcc instead of library.so ,also provide library.so's location
gcc -L/path/to/library.so -o test.o -llibrary -ldl -lpthread -lm

Trouble with implementing a basic Makefile in c

Okay so I need to make a basic Makefile for a program I wrote. Here are the files:
list.c
hash.c
order_book.c
libdefault_hash.a //provided already so I do not need to create.
I need to create libraries for list.c and hash.c so that orderbook can use them when it compiles. So this is what I currently have in Makefile:
all: orderbook
orderbook: orderbook.c liblist.a libhash.a
gcc -std=c99 -o orderbook order_book.c list.c -L. -llist -lhash -libdefault_hash
liblist.a: list.c
gcc -std=c99 -c list.c
ar rcu liblist.a list.o
libhash.a: hash.c
gcc -std=c99 -c hash.c
ar rcu libhash.a hash.o
My understanding of how makefiles work is very small but here is my thought process,
all: orderbook will mean that orderbook: will run.
orderbook.c will then compile, then the code will compile the libraries.
Once the libraries are compiled it will run:
gcc -std=c99 -o orderbook order_book.c list.c -L. -llist -lhash -libdefault_hash
And the result should be a simple program file named orderbook, but the terminal prints out:
$ make
gcc -std=c99 -o orderbook order_book.c list.c hash.c -L. -llist -lhash -libdefault_hash
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/../../../../x86_64-pc-linux-gnu/bin/ld: skipping incompatible ./liblist.a when searching for -llist
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -llist
/usr/lib/gcc/x86_64-pc-linux-gnu/4.6.3/../../../../x86_64-pc-linux-gnu/bin/ld: cannot find -libdefault_hash
collect2: ld returned 1 exit status
make: *** [orderbook] Error 1
$
Any help/guidance is much appreciated.
Let's take this in small steps. First, here's a sequence of commands that looks like what you have in mind:
gcc -std=c99 -c list.c -o list.o
ar rcu liblist.a list.o
gcc -std=c99 -c hash.c -o hash.o
ar rcu libhash.a hash.o
gcc -std=c99 -o orderbook order_book.c -L. -llist -lhash -libdefault_hash
Try these commands without Make, and see which ones work (are you sure "rcu" shouldn't be "-rcu"?). Tell us the results either by commenting on this answer or editing your question. Once any of these commands works, we can start writing the makefile.

linking pthread library issue

Am facing a problem that may be slightly complicated to explain and understand as giving the entire picture would be too big and difficult.
Please excuse me for it.
Consider the following Makefile:
all: clients.so simulator backup
LD_PRELOAD=/home/Juggler/client/clients.so ./simulator
backup: backup.c libclient.a
gcc backup.c -o backup -L /home/Juggler/client -L. -lclient -ldl
simulator: simulator.c libclient.a
gcc -g simulator.c -o simulator -L /home/Juggler/client -L. -lclient -ldl -pthread
libclient.a: libclient.o client.o
ar rcs libclient.a libclient.o client.o
libclient.o:libclient.c
gcc -c libclient.c -o libclient.o -pthread
clients.so: client.o client_invoke.o
ld -shared -o clients.so client_invoke.o client.o -ldl
client_invoke.o: client_invoke.c
gcc -Wall -fPIC -DPIC -c -g client_invoke.c
client.o: client.c
gcc -Wall -fPIC -DPIC -c -g client.c -ldl -pthread
We call function written in client.c from libclient.c and these functions in client.c make call to pthread_key_create(), pthread_setspecific..etc.
Threads are created by simulator.c and theses threads access functions written in he other files.
On doing make...Errors like the following appear.
/home/Juggler/client/libclient.a(client.o):In function 'setup_connection':
/home/Juggler/client/client.c:35: undefined reference to 'pthread_setspecific'
pthread.h has been included in both client.c and libclient.c
Would be grateful for anypointers . I understand information is very less...
Thanks
On linux, pthread functions live in the libpthread library. So you have to link to that.
The proper way, when using pthreads, is to compile and link using the -pthread , which, among other things, will link in the pthread library. You have the -pthread flag for some of your executables, but not for others, and not for your clients.so library, so add the flag where required.
Also, remember, when you are creating a shared library, you should compile the source files with the -fPIC flag.
(And, seems you are calling ld directly to produce the client.so library, you really should use gcc to do the linking.)

Resources