[SOLVED] Compiling Fortran code with gfortran within a C++ project
-
Hi everyone!
I'm working on a numerical library with Qt Creator (a C++ project using qmake). As it is with numerical projects, I have some Fortran code that I want to use in the project. I have .for files that are (currently) compiled with g++ and a header that links those objects to the C++ code.
However, when I try to actually use those functions, the compiler complains about undefined reference to gfortran... . Now, I'm pretty sure this comes from bad linking at compile time.
The question is: how can I use gfortran to compile the .for files and g++ to compile the .cpp files? Can I define a new variable in qmake, a new build step in the Qt Creator interface?
Anyhow, thanks for your time,
-
Yes, I tried the former:
@
FORTRAN_SOURCES +=
machine.for
zbesh.for \fortran.output = ${QMAKE_FILE_BASE}.o
fortran.commands = gfortran ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT}
fortran.input = FORTRAN_SOURCES
QMAKE_EXTRA_COMPILERS += fortran@Based on a "document":http://qt-project.org/doc/qt-4.8/qmake-environment-reference.html#customizing-makefile-output about qmake. However, I get
@undefined reference to `main'@
when I try to build this. I think that's because gfortran thinks I'm compiling an executable, while I'm compiling a static library. But qmake knows that:
@TEMPLATE = lib
CONFIG += staticlib@Can I pass those options to the custom compiler? There seems to be a CONFIG field for the custom compiler, but I'm not sure of the syntax.
As for the latter, adding custom build steps to compile only certain files seems complicated.
-
Or, as "this":http://stackoverflow.com/questions/5663083/linking-fortran-and-c-binaries-using-gcc suggests, could I pass the option -lgfortran selectively to the Fortran source files?
-
Or "this":http://www.yolinux.com/TUTORIALS/LinuxTutorialMixingFortranAndC.html, which suggest to use
@f77 -c testF.f
g++ -c testC.cpp
g++ -o test testF.o testC.o -lg2c@but how to make the last line happen automatically? --
-
Nevermind. I am just plain stupid. In the program that uses the library, I forgot to add the line
@LIBS += -lgfortran@In the library .pro file, one should just list the Fortran source files in the SOURCES. qmake will take care of the rest. The -lgfortran lib is also unnecessary for the library.