2

I am learning how to write C code in Linux and I am learning makefiles at a very beginner level. I am having problems when making shared libraries.

The exercise is to make a simple function calculator C program with files:

main.c
add.c
subt.c
mult.c
div.c

The names of the files define the function they do. The function in the file subt.c is in the static library:

libsubstatic.a

The function in the file mult.c is in the shared library:

libmultshared.so

For this program, I write the following makefile:

calc.exe: main.o add.o div.o libsubstatic.a libmultshared.so
gcc -o calc.exe main.o add.o div.o libsubstatic.a -Wl,-rpath,/home/ahmed/Desktop/labTask3 -lmultshared.so

main.o: main.c header.h
gcc -c main.c

add.o: add.c header.h
gcc -c add.c

libsubstatic.a: subt.o
ar cr libsubstatic.a subt.o

subt.o: subt.c header.h
gcc -c subt.c

libmultshared.so: mult.o
gcc -shared -fPIC -o libmultshared.so mult.o

mult.o: mult.c header.h
gcc -c -fPIC mult.c

div.o: div.c header.h
gcc -c div.c

The path where the code and makefile is placed:

/home/ahmed/Desktop/labTask3

I get the following message after I type "make" in the terminal:

gcc -o calc.exe main.o add.o div.o libsubstatic.a -Wl, -rpath, /home/ahmed/Desktop/labTask3 -lmultshared.so
gcc: error: unrecognized command line option ‘-rpath,’
make: *** [calc.exe] Error 1

What am I missing? Did I write this makefile correctly? Please explain shared libraries, my concept might be faulty.

Please help.

Note that, I'm new to linux and I don't have much experience in makefiles.

EDIT: I removed the spaces as directed in the first answer. Now the terminal says:

gcc -o calc.exe main.o add.o div.o libsubstatic.a -Wl,-rpath,/home/ahmed/Desktop/labTask3 -lmultshared.so
/usr/bin/ld: cannot find -lmultshared.so
collect2: error: ld returned 1 exit status
make: *** [calc.exe] Error 1

Should I do something with the "-lmultshared.so"? What should I do?

ahmedbatty
  • 29
  • 1
  • 3
  • 9

1 Answers1

4
-Wl, -rpath, /home/ahmed/Desktop/labTask3

Get rid of the spaces. This should all be one long argument.

-Wl,-rpath,/home/ahmed/Desktop/labTask3

See this excellent answer by @KerrekSB for a detailed explanation about passing arguments to the linker with -Wl.

Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • I got rid of the spaces and it now says: /usr/bin/ld: cannot find -lmultshared.so collect2: error: ld returned 1 exit status make: *** [calc.exe] Error 1 – ahmedbatty Feb 20 '14 at 18:45
  • @ahmedbatty Use `-L` to tell gcc where to find libraries if they're not in the standard locations (e.g., `/lib`, `/usr/lib`). You might try `-L.` to tell it to look in the current directory. – John Kugelman Feb 20 '14 at 18:51