Thread: Am I writing this makefile correctly?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    27

    Am I writing this makefile correctly?

    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:


    Code:
    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:


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


    Code:
    libmultshared.so
    For this program, I write the following makefile:


    Code:
    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:


    Code:
    /home/ahmed/Desktop/labTask3
    I get the following message after I type "make" in the terminal:


    Code:
    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
    What am I missing? Did I write this makefile correctly?
    Should I do something with the "-lmultshared.so"? What should I do?
    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.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Normally such a post should not be in the C forum since it is not, strictly speaking, a C question; rather it's a make/makefile question. The "Tech Board" or "Linux" sub-forum would be better. Mods may move this.

    Another quick note, nobody really uses the .exe extension in Linux. Executable status is done via file permissions (man chmod for more info). I recommend just calling the binary "calc".

    Thankfully I wrote a tutorial on shared libraries a little while back: Shared libraries with GCC on Linux - Cprogramming.com. Your makefile looks mostly fine. The error (at least, I think this is it) is that you are not telling GCC where to look for the shared library files when it links. You need to use the -L option as well as -Wl,-rpath.... Hard to say, as I don't have all the code/libraries available and don't have time to whip up a comparable example right now. The tutorial covers this a bit (steps 3 and 4), but to explain further:
    Shared libraries in GCC have two "paths" they need
    1. The path at link time. This is where the .so file is located on the build machine, so that the linker can look up the symbol info and generate the right hooks when building/linking the executable. This has nothing to do with where the file will exist when the executable is run, since it may run on a system other than the build system, which may have the library in a different location.
    2. The path at runtime. This is where the .so file will be located on the machine the executable runs on. This may be specified at build time (on the build machine), via rpath/runpath. It may also be specified on the system it runs on, either globally (via ldconfig/ld.so) or via the LD_LIBRARY_PATH variable.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    27
    I changed this:

    Code:
    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
    to this:

    Code:
    calc.exe: main.o add.o div.o libsubstatic.a libmultshared.so
        gcc -o calc.exe main.o add.o div.o libsubstatic.a -L. libmultshared.so
    so now terminal says:

    Code:
    ./calc.exe: error while loading shared libraries: libmultshared.so: cannot open shared object file: No such file or directory
    although when I type "ls", it shows that I have a file named libmultshared.so in the directory

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Try re-reading my post and the tutorial a bit more carefully:
    Quote Originally Posted by anduril462 View Post
    You need to use the -L option as well as -Wl,-rpath....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Am I writing binary data correctly
    By generaltso78 in forum C Programming
    Replies: 13
    Last Post: 03-06-2013, 03:14 PM
  2. Importance of writing a Makefile
    By AnishaKaul in forum C Programming
    Replies: 7
    Last Post: 04-29-2010, 11:08 PM
  3. writing a makefile?
    By rs07 in forum C Programming
    Replies: 7
    Last Post: 04-07-2009, 06:45 AM
  4. Writing BMP file... doesn't work correctly
    By tin in forum Game Programming
    Replies: 3
    Last Post: 12-28-2005, 04:40 AM
  5. Writing makefile in Unix
    By Mangesh in forum C Programming
    Replies: 2
    Last Post: 09-11-2001, 10:48 PM

Tags for this Thread