Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c++ Subject: Re: Trying to be generic with the linker (ld instead of g++) Date: Tue, 20 Aug 2019 15:58:53 -0700 Organization: None to speak of Lines: 48 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="a6d658d4a2a10098aaf65c730678ffc4"; logging-data="10253"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18BxWc/WTbjS7BoiuDQ2Bxg" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:GUs/DvlmnfkR4hGiSyYrBx4VNPE= sha1:tKvg68GfankfcgsnE4rwrokkktg= Xref: csiph.com comp.lang.c++:67826 Frederick Gotham 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 Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */