gcc-problem-link library chain still exists "undefined reference to"?

Source: Internet
Author: User
Tags function definition
gcc-problem-link library chain still exists "undefined reference to"? Description

These days when performing some makefile found that the same link library link when the compile command, under different Linux platforms under different versions of GCC show different effects. Their code is the same, linked to the library are the same, the command is the same, one is successful link, and the other is an error, hint has undefined reference, that is, "undefined reference to XXXX." Such problems are generally missing link library prompts, as long as the addition of-lxxx added on the libxxx can be resolved, up to add-l/path/to/lib add the correct search library path, but I encountered the situation is still unable to resolve. In fact, the hint "undefined reference" indicates that the library with this function definition has no link success, and after careful investigation, it turns out that in some GCC versions, this actually has something to do with the location of the link library in the compiled or linked commands. Experiment

Give a piece of experimental code, it needs to link Pthread library:

#include <stdio.h>

#include <pthread.h>
#include <unistd.h>

void* Print (void* PArgs) {
    printf ("The sleep.\n");
    Sleep (3);
    printf ("Just print!\n");
    return NULL;
}

int main () {
    pthread_t id;
    Pthread_create (&id,null,print,null);
    Pthread_join (id,null);
    return 0;
}

We compile in two different platform environment, the system is 64-bit Ubuntu and 64-bit redhat, all use gcc--version display respectively as:
Ubuntu
GCC (Ubuntu 4.8.2-19ubuntu1) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; The source for copying conditions. There is NO
Warranty Not even for merchantability or FITNESS for A particular purpose.

Redhat
GCC (GCC) 4.8.5
Copyright (C) 2015 free Software Foundation, Inc.
This is free software; The source for copying conditions. There is NO
Warranty Not even for merchantability or FITNESS for A particular purpose.

Compilation build commands are used:

Gcc-lpthread TEST_GCC.C

Redhat normally passes and runs correctly, while Ubuntu shows:

TEST_GCC.C: (. text+0x50): Undefined reference to ' pthread_create '
test_gcc.c: (. text+0x61): Undefined reference to ' Pthread_join '
Collect2:error:ld returned 1 exit status

The reason for this is that-lpthread this instruction is placed in front of TEST_GCC.C, for the redhat version of GCC, this is not a problem, and for the lower version of Ubuntu problem, there is a problem, you must put the linked library in the need to link the object ( XXX.C or XXX.O), GCC is able to link correctly, and the compilation command is replaced by the following:
GCC Test_gcc.c-lpthread
So there is no problem, so in writing makefile or write compile link command, the link library written in the back to ensure compatibility. Conclusions

Therefore, when compiling commands such as GCC, you can consider the position of various components in the command if such errors occur.
In particular,there is a default implicit inference rule in the makefile . For example, many XXX.O to generate executable targets, it also has the corresponding macro variables to link to the library, it is likely to directly write the link library in the need to link the object before, resulting in "undefined reference" error , so let's make it clear that we're not using implicit derivation rules (who knows if each version makes the same thing), and that it's good to write the link commands yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.