Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #67826

Re: Trying to be generic with the linker (ld instead of g++)

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c++
Subject Re: Trying to be generic with the linker (ld instead of g++)
Date 2019-08-20 15:58 -0700
Organization None to speak of
Message-ID <lnd0gz30lu.fsf@kst-u.example.com> (permalink)
References <fd41e1db-a2dc-445e-8ecb-f176d3361e03@googlegroups.com>

Show all headers | View raw


Frederick Gotham <cauldwell.thomas@gmail.com> writes:
> I've been doing web searches all morning to try solve this so I'm here
> now as last resort.
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Typically if we want to combine C and C++ in one program, we can do
> three steps:
>
> gcc -c *.c
> g++ -c *.cpp
> g++ -o prog *.o
>
> So we use 'gcc' to compile the C code. Then we use 'g++' to compile
> the C++ code. Finally we use 'g++' a second time to link the object
> files together into one executable file.
>
> I'm trying to be as generic as possible here in the final step
> (i.e. linking), and so I want to replace the second use of 'g++' with
> 'ld', so something like:
>
> gcc -c *.c
> g++ -c *.o
> ld -o prog *.o
[...]

If you're doing this as a learning exercise, it's perfectly reasonable
(and others have already suggested "g++ -v").  If you're trying to do
something useful, I don't think this is a good approach.

g++ is a driver program that invokes other programs, including ld, as
needed.  It knows what linker options are required to link a C++
program.  That's its job.  Why reinvent the wheel?

You also run the risk that the required options could change with
new releases, or with different versions of installed tools, or
with the phase of the moon.  Knowing how g++ invokes ld doesn't
necessarily tell you how g++ figured out how to invoke ld, or how
that might change.

(On the other hand, if you need to link together code in different
languages, say C++ and Ada, you might need to dive a bit more deeply
into the innards.)

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Trying to be generic with the linker (ld instead of g++) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-08-20 04:09 -0700
  Re: Trying to be generic with the linker (ld instead of g++) Bart <bc@freeuk.com> - 2019-08-20 12:25 +0100
    Re: Trying to be generic with the linker (ld instead of g++) Frederick Gotham <cauldwell.thomas@gmail.com> - 2019-08-20 04:34 -0700
      Re: Trying to be generic with the linker (ld instead of g++) Chris Vine <chris@cvine--nospam--.freeserve.co.uk> - 2019-08-21 01:07 +0100
    Re: Trying to be generic with the linker (ld instead of g++) Keith Thompson <kst-u@mib.org> - 2019-08-20 15:50 -0700
      Re: Trying to be generic with the linker (ld instead of g++) Bart <bc@freeuk.com> - 2019-08-21 00:27 +0100
  Re: Trying to be generic with the linker (ld instead of g++) Ralf Fassel <ralfixx@gmx.de> - 2019-08-20 14:05 +0200
  Re: Trying to be generic with the linker (ld instead of g++) David Brown <david.brown@hesbynett.no> - 2019-08-20 14:10 +0200
  Re: Trying to be generic with the linker (ld instead of g++) Keith Thompson <kst-u@mib.org> - 2019-08-20 15:58 -0700
  Re: Trying to be generic with the linker (ld instead of g++) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-22 04:47 -0700
    Don't extensions still work? (Was: Trying to be generic with the linker (ld instead of g++)) gazelle@shell.xmission.com (Kenny McCormack) - 2019-08-22 12:12 +0000
      Re: Don't extensions still work? (Was: Trying to be generic with the linker (ld instead of g++)) David Brown <david.brown@hesbynett.no> - 2019-08-22 15:08 +0200
      Re: Don't extensions still work? (Was: Trying to be generic with the linker (ld instead of g++)) Tim Rentsch <tr.17687@z991.linuxsc.com> - 2019-08-22 12:57 -0700

csiph-web