Including libraries in project?

This is a discussion on Including libraries in project? within the C Programming forums, part of the General Programming Boards category; Hi I was having a problem building some code (the compile went fine, but I got unresolved externals when I ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    18

    Including libraries in project?

    Hi
    I was having a problem building some code (the compile went fine, but I got unresolved externals when I tried to link). I asked around on the mailing list for the library I was using, and someone told me I should try to include the libs in my project. I thought that this was the more appropriate place for asking exactly what that meant? I'm using a dinky commandline compiler (gcc on windows), so I don't think I can build a "project" per se, but what is the equivalent? Thanks for your time

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    Can you be more specific than "some library" and "some error messages" ?

    Code:
    $# the problem
    $ gcc foo.c
    /tmp/ccm08DZS.o(.text+0x2c): In function `main':
    foo.c: undefined reference to `sin'
    collect2: ld returned 1 exit status
    
    $# the solution is link with the library containing 'sin'
    $ gcc foo.c -lm
    
    $# the program
    $ cat foo.c
    #include <stdio.h>
    #include <math.h>
    
    int main() {
      printf("%f\n", sin(0.5) );
      return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    More of a GCC question

    It's already been explained to you. If you have a library called libm.a that you program needs, you either put the library in the library path (ugh) or add a directory to the library path with -L, and use -l (lowercase 'L') to link with the library:
    Code:
    C>gcc math.c -o math.exe -L C:\libraries -lm
    If your library is called libSDL.a, then you use -lSDL, and so on.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    18
    Thanks all. I have another problem now. Here is my setup: The library I'm using is called libtiff.
    Using cygwin on XP.
    The files (libtiff.def, libtiff.dll.a, libtiff-bcc.lib, libtiff.lib) are in c:\cygwin\usr\tiff\lib.

    The files (tiff.h, tiffio.h, tiffconf.h, tiffvers.h) are in c:\cygwin\usr\tiff\include

    Then there a bunch of things in c:\cygwin\usr\tiff\bin: some dll's and some executables, including libtiff3.dll. Everything else in the tiff folder is documentation etc (I think).

    I'm compiling with:
    gcc -o image prog1.c -I/usr/tiff/include -L/usr/tiff/lib /usr/tiff/lib/libtiff.lib
    I tried using the -l switch, but it didn't work out. Anyways, now it compiles and links, but when I try and run it I get a window saying that the application has failed to start because libtiff3.dll could not be found. I tried using
    appending /usr/tiff/bin/libtiff3.dll to the command, but that changed nothing. Thanks for your help
    I don't suppose it matters, but the code just #includes tiffio.h, where the functions I'm calling are defined, and uses them once.
    Also, I got this library as binaries; if nothing works out, would anyone recommend trying to build it myself?

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    18
    Never mind; I just copied the dll to the same folder as the executable and it ran. Now I just I have a segmentation fault to deal with...
    Thanks everyone

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Never mind; I just copied the dll to the same folder as the executable and it ran.
    DLLs are different; they just have to be in the path when the executable is run. If you have many applications that use the same DLL you can put the DLL in \windows\system[32] to save space.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Including "Groups" of Libraries
    By shoover in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2008, 03:59 AM
  2. MinGW thread-safe runtime libraries
    By Mario F. in forum C++ Programming
    Replies: 3
    Last Post: 08-21-2006, 09:15 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 08:41 PM
  4. Please, suggest a project .
    By Dragon227Slayer in forum Tech Board
    Replies: 1
    Last Post: 06-12-2004, 11:48 AM
  5. DJGPP project problems
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-08-2002, 08:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21