public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Dingjun Chen <Dingjun.Chen@geotechairborne.com>
Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: How does g++ link an objective file .o built by gcc?
Date: Thu, 4 Jan 2024 18:53:05 +0000	[thread overview]
Message-ID: <CAH6eHdTDhRv3D7opr6tDFnE-HWF97EKDC3iZXuvqn+CQEv7nQg@mail.gmail.com> (raw)
In-Reply-To: <YT2P288MB0073D6BA65387A01FE963AB297672@YT2P288MB0073.CANP288.PROD.OUTLOOK.COM>

On Thu, 4 Jan 2024 at 18:48, Dingjun Chen
<Dingjun.Chen@geotechairborne.com> wrote:
>
> HI, everyone,
>
> I have a question for you.
>
> I have multiple c++ files .cc (main program is C++) and one c file 'librtd-dm6620.c'. Of course I will use g++ to build the executable. For that 'librtd-dm6620.c' file I still want to build its .o file via gcc because there are some possibly fatal warnings if I use g++ to build this .c file. If this .c file is built with gcc, there is no any warning and it is perfect in compilation.

Then that means you are trying to compile C code using the C++
compiler. That might work sometimes, for some C code, but will often
not work. C and C++ are different languages.


>
> Finally, I want to build the executable by linking all .o files via g++.  The problem is that the .o file built by gcc is not really linked by g++.  All defined functions in 'librtd-dm6620.c' cannot be found by the g++ linker 'ld'.

That's because the C++ compiler uses "name mangling" when it compiles
functions, so that the symbol names in the .o are different when
compiled as C++.

For example, a function void f() { } will be compiled to a symbol
called "f" when compiled as C, but a symbol called "_Z1fv" when
compiled as C++.

You either need to compile all your C files as C++, so that the symbol
names are all consistent, or you need to tell the C++ compiler not to
mangle the names, like so:

#ifdef __cplusplus
extern "C" {
#endif
void f() { }
#ifdef __cplusplus
}
#endif

When this is compiled by a C++ compiler it will produce a symbol
called "f", just like when compiled by a C compiler.

The simplest answer is probably just to stop trying to compile C files
as C++ code. Either write C++, or write C, don't write C and then
compile as something different.

  reply	other threads:[~2024-01-04 18:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-04 18:47 Dingjun Chen
2024-01-04 18:53 ` Jonathan Wakely [this message]
2024-01-04 19:30   ` Dingjun Chen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAH6eHdTDhRv3D7opr6tDFnE-HWF97EKDC3iZXuvqn+CQEv7nQg@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=Dingjun.Chen@geotechairborne.com \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).