Thread: Having problems with Makefile

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    42

    Having problems with Makefile

    I'm trying to create a dynamic link library using the following Makefile. The library creation works just fine, however app/LAB5exe never gets created and I'm never given any errors. I'm quite new at working with makefiles so I don't understand why its not getting made. Any help would be greatly appreciated. Thank you!!

    Code:
    #Make file to create Recursion api
    
    
    lib/libRecursionapi.so: lib/DIV.o lib/MUL.o lib/EXP.o lib/GCD.o
            gcc -shared -o lib/libRecursionapi.so lib/DIV.o lib/MUL.o lib/EXP.o lib/GCD.o
    app/LAB5exe: lib/LAB5.c lib/libRecursionapi.so
            export LD_LIBRARY_PATH=./lib
            gcc lib/LAB5.c -L./lib -lRecursionapi -o app/LAB5exe -ldl
    DIV.o: lib/DIV.c
            gcc -fPIC -c lib/DIV.c
    MUL.o: lib/MUL.c
            gcc -fPIC -c lib/MUL.c
    EXP.o: lib/EXP.c
            gcc -fPIC -c lib/EXP.c
    GCD.o: lib/GCD.c
            gcc -fPIC -c lib/GCD.c

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    "app/LAB5exe" never gets made by default because what "make" does by default is create the first target, which is lib/libRecursionapi.so. That target does not list LAB5 as a prerequisite, so there is no need to make it. If you try "make app/LAB5ext" (or make it a prereq for the default target) , then it will happen.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    42
    thanks. I tried just moving the app/LAB5exe to a new makefile and that fixed the problem. However now its telling me:

    app/LAB5exe: error while loading shared libraries: libRecursionapi.so: cannot open shared object file: No such file or directory

    I would think I'm doing this wrong, but I actually had it working just fine, but I'm required to have a specific directory structure, and when I went to move the files around thats when I started getting more errors. Since I'm rather new at this, i can't tell where I'm going wrong. When I was having the above problem before, I solved it with
    Code:
     export LD_LIBRARY_PATH=./
    but now I'm getting it again, and I changed the LIBRARY PATH to where my lib is located, still no love....

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Using "export" in the makefile is not a very good solution because it has no permanence and only affects your current shell and shells subsequently spawned from it. Also, you are overwriting the existing path, which is probably not what you want to do. When you add to a path, retain the existing value as well:

    Code:
    PATH=$PATH:/new/path
    You can use stuff like `pwd` in makefiles, btw, which would be better because it yields the absolute path. Also: have you made a .so before? I think you are missing some compiler switches, or at least, the way I learned to do it included:

    Code:
    gcc -shared -Wl,-soname,libwhatever.so.1 -o libwhatever.so.1.0 whatever.o
    Anyway, you should do this with one makefile. If you don't want to add LAB5 as a prereq for the libRecursionapi (because it really isn't), you can certainly make libRecursionapi a prereq for LAB5, and then just make LAB5 the first (ie, default) target. WRT linking:

    Code:
    libdir = `pwd`/lib
    Now you can use "-L$(libdir) -lRecursionapi". You probably want to do a "ldconfig $(libdir)" first tho (ie, at the end of the recipe for the .so).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Code:
    export LD_LIBRARY_PATH=./
    This would blow out any other paths you might have in LD_LIBRARY_PATH. It's probably empty, but to be safe, you should do something like the following to prepend your custom library path to the beginning of LD_LIBRARY_PATH.
    Code:
    export LD_LIBRARY_PATH="./lib:${LD_LIBRARY_PATH}"
    I haven't done shared library stuff in a while, so I'm not totally sure on this, but you may need to run ldconfig after you build your .so file. I think you're fine with a single make file, but we can clean it up a bit:
    Code:
    default: app/LAB5exe
    
    lib/libRecursionapi.so: lib/DIV.o lib/MUL.o lib/EXP.o lib/GCD.o
            gcc -shared -o $@ $^
            ldconfig -n ./lib
    
    app/LAB5exe: lib/LAB5.c lib/libRecursionapi.so
            gcc lib/LAB5.c -L./lib -lRecursionapi -o app/LAB5exe -ldl
    
    %.o : lib/%.c
            gcc -fPIC -c $<
    That's all done by hand, so there may be some small errors, but it should be more or less what you want.
    That ldconfig sets up the necessary symlinks for shared libraries. Check the man page for more details.
    The $@ refers to the name of the target.
    The $^ refers to all prerequisites for that rule.
    The % acts like a wildcard character, and whatever fills it in on the left side of the colon also fills it in on the right side. So %.o expands in one case to DIV.o, which depends on lib/DIV.c. It does likewise for MUL.o, EXP.o and GCD.o.
    The $< refers to the name of the first prerequisite for that rule (e.g. lib/DIV.c).

    Make files can be a bit scary, but once you get over the fear, their kinda cool And the online help is pretty good: GNU `make'.
    Last edited by anduril462; 05-04-2011 at 12:41 PM. Reason: improved rule for lib/libRecursionapi.so

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problems with makefile
    By bartleby84 in forum C Programming
    Replies: 4
    Last Post: 02-16-2009, 09:02 AM
  2. makefile problems
    By cpjust in forum C++ Programming
    Replies: 4
    Last Post: 06-16-2008, 03:14 AM
  3. difference makefile makefile.am makefile.in
    By Bargi in forum Linux Programming
    Replies: 7
    Last Post: 10-28-2007, 02:08 PM
  4. help with makefile
    By ICool in forum C Programming
    Replies: 31
    Last Post: 10-22-2007, 10:03 AM
  5. Makefile
    By M-21 in forum Linux Programming
    Replies: 1
    Last Post: 02-13-2002, 06:55 AM