ERROR collect2: ld returned 1 exit status

This is a discussion on ERROR collect2: ld returned 1 exit status within the C++ Programming forums, part of the General Programming Boards category; I am getting this message when building a application but do not have any idea what is causing the problem. ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    ERROR collect2: ld returned 1 exit status

    I am getting this message when building a application but do not have
    any idea what is causing the problem. The output that goes with it is
    just the normal output of the g++ command. I have tried the --verbose
    cli flag to the g++ command but this gives no further information.

    Any ideas on how I can get more information from ld and collect2 to see
    what the problem is?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    USUALLY this means that you have unresolved symbols or missing input files. Do you not get ANY indication of a problem? I suspect the verbose flag will just confuse you.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    If there are unresolved symbols or missing input files, I think compiler should throw error(s) at compiling-time, not linking-time

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Unresolved symbols are linking errors. Meaning the linker was unable to find the symbol and thus can't generate any code for it.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    thanks. but what do you mean "missing input files"

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Usually, you did not add a .c or .cpp file to your project. The compiler will declarations of your functions and think they exist, but the linker won't find them because the compiler didn't compile the code for the functions.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thank you, Elysia. One more question:
    You mentioned that this error is thrown when I use a symbol/function but never declare/define it. Now if I declare it but never define it, will that error be thrown?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    If a symbol is not declared, the compiler will complain.
    If a symbol is declared but never defined, the linker will complain.

  9. #9
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Let me summarize your points about a symbol's declaration, definition, and using:
    If a symbol is not declared, whether you use or define it, the compiler will complain.
    If a symbol is declared but never defined, whether you use it or not, the linker will complain.
    If a symbol is declared and defined, whether you use it or not, the linker will NOT complain.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Yes to all three statements.
    Although to your first question, there is an exception. If the function you are trying to call is defined above the function you're calling it from, you don't have to declare it. The compiler will generate code as it should.
    HOWEVER, this is really bad practice, so don't do it.

    Also note that this applies to normal functions, classes and variables, but not templates.
    Last edited by Elysia; 12-03-2007 at 11:35 AM.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Actually, the linker won't complain if a symbol is declared but not defined, but you don't actually use it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Kind of depends on what kind of symbol it is. I don't think it's the same for all. Like an extern global variable will create a linking error if you don't define it, I believe?
    Nevertheless, it's just good practice to define everything you declare. The declarations are just there for the compiler, after all, and if you're telling the compiler they exist, then you should make sure they exist too.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Kind of depends on what kind of symbol it is. I don't think it's the same for all. Like an extern global variable will create a linking error if you don't define it, I believe?
    Nevertheless, it's just good practice to define everything you declare. The declarations are just there for the compiler, after all, and if you're telling the compiler they exist, then you should make sure they exist too.
    Yes, it's more about for example the difference between including stdio.h, and actually using all the dozens of functions in stdio.h - most programs only use half a dozen of those functions, but you CAN use rewind, ungetc, unlink, fwprintf, etc, etc if you really want to.

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by meili100 View Post
    I am getting this message when building a application but do not have
    any idea what is causing the problem. The output that goes with it is
    just the normal output of the g++ command. I have tried the --verbose
    cli flag to the g++ command but this gives no further information.

    Any ideas on how I can get more information from ld and collect2 to see
    what the problem is?
    Try adding -Wl,-V (exactly as I've written it) to the gcc options to turn on verbose linker output.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. cannot find -lpcap collect2: ld returned 1 exit
    By nasim751 in forum C Programming
    Replies: 0
    Last Post: 02-11-2008, 11:37 PM
  3. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  4. Troubles with Sockets
    By cornholio in forum Windows Programming
    Replies: 6
    Last Post: 10-26-2005, 05:31 AM
  5. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM

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