Thread: Linking libraries when compiling with g++ in ubuntu

  1. #1
    Registered User
    Join Date
    Aug 2010

    Linking libraries when compiling with g++ in ubuntu

    I have a program and I'm trying to compile it using g++.

    I have some header files with functions I need that are part of a library that someone else wrote that I included in my program. However, upon compiling, I receive the error "undefined reference to <insert constructor in header file>". Yet upon checking with the constructors, I am constructing the object correctly.

    Upon doing some research I found a possible solution to my problem - that I needed to link my library to my program. My library included a makefile and I have a library file with an '.a' extension, but I don't know how to tell g++ to use the library when compiling.

    So that's what I came here for. Obviously I'm a novice to c++ coding and to using g++. If someone could let me know how to include the library when compiling, or if you think my problem is of a different origin, I would appreciate the help.

    Also (assuming I'm correct in what's wrong), I would appreciate an explanation as to why I need to "link" the library when compiling. My impression is that including a list of header files was sufficient to access the functions in those files.


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Posting your actual g++ command line(s) would help.

    Posting some actual error messages as well would be useful.

    g++ prog.cpp -llibrary
    compiles prog.cpp, and links it with a file called liblibrary.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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. GCC: Compiling with both static and shared libraries
    By eatwithaspork in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 01:48 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 6
    Last Post: 08-21-2006, 06:01 PM
  5. 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