linking to libraries

This is a discussion on linking to libraries within the C Programming forums, part of the General Programming Boards category; This is my first real project with C so bare with me. I am trying to write a program that ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    linking to libraries

    This is my first real project with C so bare with me.

    I am trying to write a program that will be able to, among other things, read the geo tags of a tiff using the libgeotiff library. I downloaded and installed the library, and its dependent, libtiff, from source.
    How do I include these now into my program? If I do an #include "absolute path", I can get the .h file, and use the type definitions, but when I call any of the functions, I get a symbol(s) not found error.

    I am using Xcode on mac os x.

    Thank you

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    What compile are using?? If you’re on gcc then you could -l flag to link the libraries or perhaps you also specify the library path explicitly. The following are the few examples

    Explicitly specifying
    Code:
    gcc -o myCode -Wall myCode.c  /usr/lib/libm.a
    using -l flag
    Code:
    gcc -o myCode -Wall myCode.c  -lm
    As you could see I was trying to link the maths library. The –lName flag will allow you link the libraries.

    Name is nothing but the libm.a. You need to find the the name of the library and try link them this way. That should probably work!

    ssharish

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    > If I do an #include "absolute path", I can get the .h file
    Just use regular < >


    Then this command line
    gcc -I/path/to/header -L/path/to/lib prog.c -lmylib

    Where
    /path/to/header/file.h is a file you want to include - as #include <file.h>
    /path/to/lib is where you'll find libmylib.a
    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.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    Thank You

    All of those options work from the command line, however, there's a little more to get that going within Xcode. Just in case anyone else has the same problem as I, here is what you need to do.

    to do so you need to right click on the target (on the left menu under Targets) and click get info
    from there, scroll down to header Search Paths and enter the location of the header, and then in Library Search Paths, enter the path of the .a file. This is equivalent of -I/path/to/header -L/path/to/lib.
    After that you need to drag the .a file into the files list, which is equivalent of -lmylib.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with linking to other c++ libraries via .net
    By pipercubusa in forum C++ Programming
    Replies: 0
    Last Post: 05-02-2005, 01:08 PM
  2. Linking and Libraries
    By krygen in forum C++ Programming
    Replies: 14
    Last Post: 12-26-2004, 02:01 PM
  3. Two questions (Linking Libraries and Creating Them)
    By Thantos in forum Linux Programming
    Replies: 3
    Last Post: 03-21-2004, 04:01 PM
  4. Dynamically linking libraries
    By neandrake in forum Windows Programming
    Replies: 5
    Last Post: 06-18-2003, 02:54 PM
  5. Linking .h files to libraries
    By sean in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2001, 08:20 PM

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