top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Linking multiple languages

+1 vote
345 views

If I compile c++ code, I am supposed to use g++. This automatically does some magic that calling just Gcc would not do. The same with Fortran code and gfortran. So my question is which frontend should I use to link if I have object files from both c++ as well as Fortran, c, and maybe another language still?

posted Aug 31, 2013 by Deepankar Dubey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

You can really do it in whatever way you want. If you have an application with mixed src code, you could compile it as FORTRAN and link to the c library, or compile as c and link to the FORTRAN library.
Whether you compile as c or c++ doesn't make any real difference.

You need separate compile rules for each compiler (c, c++, FORTRAN). These will compile the objects from your mixed src. Then you need a build rule the will link the objects, including one of the libraries.

Here is a sample, where your src is,

.src/main.cpp
.src/MY_FUNCTION1.FOR
.src/my_function2.cpp
.src/my_function2.c

SOURCELOC = ./src
BDIR = ./bld

# mixed app objects objects
MIXED_OBJS = 
 $(BDIR)/main.o 
 $(BDIR)/MY_FUNCTION1.o 
 $(BDIR)/my_function2.o 
 $(BDIR)/my_function3.o

# build my_app, build as gfortran and link to c library
$(BDIR)/my_mixed_app.exe: $(MIXED_OBJS)
 gfortran -o $@ $(MIXED_OBJS) -lstdc++

# compile src gfortran objects with FORTRAN preprocessor
$(BDIR)/%.o: $(SOURCELOC)/%.FOR
 gfortran -c -o $@ $<

# compile src c++ objects
$(BDIR)/%.o: $(SOURCELOC)/%.cpp
 g++ -c -o $@ $<

# compile src c objects
$(BDIR)/%.o: $(SOURCELOC)/%.c
 gcc -c -o $@ $<

You should also be able to compile your .c files as g++, but since you are using the file extension to indicate the compiler, you will need a rule that lets make know to use g++ for .c.

# compile src c objects
$(BDIR)/%.o: $(SOURCELOC)/%.c
 g++ -c -o $@ $<

You could do the same thing with a link rule to build as g++ and link to the fortran library.

# build my_app, build as g++ and link to fortran library
$(BDIR)/my_mixed_app.exe: $(MIXED_OBJS)
 g++ -o $@ $(MIXED_OBJS) -lgfortran

If I remember right, one of the reasons to build as FORTRAN and link to the c library is that many systems may not have a FORTRAN runtime library installed, but most will have a c library somewhere. I'm not 100% sure about that, so perhaps someone else will comment.

answer Aug 31, 2013 by Anderson
+1 vote

You can probably use gcc but explicitly link in each language's support library.

answer Aug 31, 2013 by Ahmed Patel
Similar Questions
+5 votes

GCC is stripping the object file when nothing from the file is being used anywhere in the executable (even -o0 does nor help). We have to disable this feature of linker g++. could any one please help us to resolve this issue??

+2 votes

I am getting following error when compiling my program with gcc version 4.9.0

gcc --version
gcc (GCC) 4.9.0 20131023 (experimental)

Error

./A.out: /usr/lib/x86_64-linux-gnu/libstdc++.so.6: version `CXXABI_1.3.8' not found (required by ./A.out)

But I gcc 4.7.3 is fine. Any guess why this may be happening.

0 votes

I recently encountered a problem with a function was marked as a constructor. The source file was compiled with -O0, but it appears the function was optimized to the point it skipped some of the startup
code and jumped into the failure state (which called exit). I was able to restore desired behavior with '#pragma GCC optimize("O0")' around the function (even volatile tricks did not help).

The startup code had to do with an integrity check. The expected fingerprint was back-patched after compiling, and then recalculated at runtime. Then, a memcmp was made. It appears the compiler deduced that the allocation was a string of 0's and could never be equal to the runtime fingerprint, so its just omitted the code.

Is this expected behavior for functions marked as constructors (compiled with -O0)?

0 votes

Reading the gcc 5.1 manual it seems that VLA parameters must be accepted by g++. For example the following is fine when compiled with gcc --std=gnu11

float sum (int M, float x[M]) {
 return x[0] + x[1];
}

int main (void) {
 float xs[2] = {1, 2};
 float acc = sum (2, xs);
 return 0;
}

But it is rejected by g++ --std=gnu++11. Is this possible at all with g++?

+2 votes

I want to build a Python based user interface for an existing Fortran application (as everyone wants to do ;)). Lets assume the application is parametrised via cmdline parameters and it calculates tons of numbers
that have to find their way into the Python UI. There are several ways to achieve this: using stdin/stdout to exchange data, using files, converting the application to a library and load that from Python, etc.

I thought about equipping the Fortran application with sockets, so that I can send input data and commands (which is now done via cmd line) and reading output data back.

Any thought on this or any best practices?

...