public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106483] New: undefined reference to function implemented in shared library if putting main.cpp after options, but non of any error when putting before options
@ 2022-07-30 12:30 xyang619 at 126 dot com
  2022-07-30 13:20 ` [Bug c++/106483] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: xyang619 at 126 dot com @ 2022-07-30 12:30 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c++/106483] undefined reference to function implemented in shared library if putting main.cpp after options, but non of any error when putting before options
  2022-07-30 12:30 [Bug c++/106483] New: undefined reference to function implemented in shared library if putting main.cpp after options, but non of any error when putting before options xyang619 at 126 dot com
@ 2022-07-30 13:20 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-07-30 13:20 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Firstly, this is the linker doing this, not GCC. GCC just passes the arguments
to the linker in the order you specify them.

Secondly, this is the correct behaviour. Your linker is set up to default to
--as-needed which means that shared libraries on the command line will only get
used if they satisfy a non-weak undefined reference from a regular object file
that has already been processed by the linker. If you put -ladd before main.cpp
then there are no undefined symbols yet, so the library is just ignored.

https://web.archive.org/web/20210609020437/c-faq.com/lib/libsearch.html

Either link with -Wl,--no-as-needed to change the linker's behaviour so that
shared libraries are always used, or put the libraries in the right order.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-07-30 13:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-30 12:30 [Bug c++/106483] New: undefined reference to function implemented in shared library if putting main.cpp after options, but non of any error when putting before options xyang619 at 126 dot com
2022-07-30 13:20 ` [Bug c++/106483] " redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).