CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2008
    Posts
    9

    [Static Libraries Linking] undefined reference to

    Hello, I say for first I'm a beginner and this is the first time I try to link a static library (created by me) in a test application. I've looked for a solution over hundreds of sites but nothing fits my situation or works as well.
    Here's the problem:
    I compile file.cpp as static library and I get libfile.a which seems as a nice static library at all. Now, on the mainApp.cpp, what should I do? I wrote #include "file.h" (a header file with signatures of the file.cpp that I put in the same folder) and I tried to compile with g++ using -L"home/user/folder" (main folder where library and app are) and -lfile but when compiling it gave a lot of errors on mainApp.cpp saying every object defined in the library were underfined references.
    E.g.
    undefined reference to "VError::Message()"

    When looking for a solution I noticed it is a very common problem for beginner programmers but there's no clear explanation also on tutorial and how-tos.

    Thanks for interesting.

    valleyman

  2. #2
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: [Static Libraries Linking] undefined reference to

    Off the top of my head...
    Code:
    g++ -c mainApp.cpp
    g++ mainApp.o library.a -o mainApp
    should do the trick...

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: [Static Libraries Linking] undefined reference to

    Also, start using an IDE. As the application grows big and you have more libs (yours and 3rd party), the command line gets longer and unmanageable. makefiles are good too but very tricky to get right. IDE's do that job for you.

    Recently, I tried out NetBeans 6.5 and it looked very promising. Very nice. I did that on windows though with cygwin installed having g++/mingw, gdb as debugger, GNU make utility for makefiles all integrated into the IDE. That's just a one time setup. Then there are other IDEs too like : code::blocks, Eclipse which are good too. And they work on Linux as well as windows.

    Since, you are using g++ hence I provided the above options (assuming you are working on Unix/Linux). If you are actually using mingw port on windows - I would also recommend on using Visual C++ express editions that are freely available. It is stripped version as compared to the professional ones that need to be bought though.

  4. #4
    Join Date
    Oct 2008
    Posts
    9

    Re: [Static Libraries Linking] undefined reference to

    @Andreas Masur: thanks for the help, but compiling that way I still get the same bunch of errors

    @exterminator: I usually use Eclipse, but now I started compiling the files on my own to simplify the process as I got the same errors with Eclipse integrated compiler (g++).

    any ideas??

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: [Static Libraries Linking] undefined reference to

    Quote Originally Posted by valleymannn View Post
    @exterminator: I usually use Eclipse, but now I started compiling the files on my own to simplify the process as I got the same errors with Eclipse integrated compiler (g++).
    1) Maybe you didn't specify the static library to use in Eclipse correctly.

    2) Maybe there truly are no functions that you claim are in the static library. Just because a static library exists that you created doesn't mean that those functions are actually there. Maybe they are in a namespace and you forgot to specify them as being in a namespace in your source code, or vice-versa, or there is a preprocessor symbol that needs to be defined in your main() app, who knows. The possibilities are endless...

    Basically, we have to believe what you're telling us, as we do not have access to your machine or files that you say you're compiling. Therefore you should provide as much detail as possible in what you're doing. Too many times, posters who claim they're doing "this and that" are not doing "this and that".

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Oct 2008
    Posts
    9

    Re: [Static Libraries Linking] undefined reference to

    youre right but I'm not saying some of the functions of the library are unrecognized, I'm *all* of them aren't, and those are functions which perfectly worked when the big library source code was in the same file where main() function is.
    I know that this still doesn't say anything but this is the only way I can explain withouth showing the whole situation (the program is about 1 thousand lines long, pretty long to synthetize) but for those who might be available to help I could send the code to let them understand better...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured