https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106483

            Bug ID: 106483
           Summary: undefined reference to function implemented in shared
                    library if putting main.cpp after options, but non of
                    any error when putting before options
           Product: gcc
           Version: og10 (devel/omp/gcc-10)
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xyang619 at 126 dot com
  Target Milestone: ---

GCC version and OS: gcc version 10.2.1 20210110 (Debian 10.2.1-6)
Code example to reproduce the bugs:
#add.h
int add(int, int);

#add.cpp

#include "add.h"
int add(int a, int b) {
        return a+b;
}

#main.cpp
#include "add.h"
#include <cstdio>
int main() {
        int a=2;
        int b=3;
        int c=add(a,b);
        printf("%d\n", c);
        return 0;
}

#first, compile add.cpp to shared library, it works well
g++ -Wall -fPIC -shared -o libadd.so add.cpp

#second, compile main.cpp and link with the libadd.so, it will produce error as
followed:
g++ -Wall -L. -ladd -o add_main main.cpp
/usr/bin/ld: /tmp/ccCzqGbU.o: in function `main':
main.cpp:(.text+0x21): undefined reference to `add(int, int)'
collect2: error: ld returned 1 exit status

#however, if I put the main.cpp before the options, it works well
g++ main.cpp -Wall -L. -ladd -o add_main

Reply via email to