Is there a way to "intelligently" link object files from diferent languages into a final executable?

Viewed 101

Hello (I feel like this question doesn't really make sense, so if it doesn't, let me know and I'll try and clarify).

I've been at this for a while, so I figured I'd reach out for help. Let's say I have three source files (the actual source code doesn't matter): main.cpp (C++), log.c (C), math.f90 (Fortran). Then, I compile them into object files:

g++ -c -o main.o main.cpp
gcc -c -o log.o log.c
gfortran -c -o math.o main.f90

What is the best way to link these object files together "intelligently"? By intelligently I mean linking the object files together with flags that would normally be passed if the linker was being called by the compiler (e.g. the compiler passes -lc++ when calling the linker).

For example, if I was compiling main.cpp into a binary in one command (g++ main.cpp) then it calls the linker like this: "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -no_deduplicate -dynamic -arch x86_64 -macosx_version_min 10.14.0 -syslibroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -o a.out /var/folders/lp/dhft80p975dd8k_60h616xmh0000gn/T/test-c9d010.o -L/usr/local/lib -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/lib/darwin/libclang_rt.osx.a. If I was to do that with the C code and the Fortran code, it would call the linker differently.

I need to know how to know to compile and link these source files so that the compiler steps in and supplies some extra linker-flags.

0 Answers
Related