Why is this project not linked?

there is main.cpp:

#include <stdio.h> extern "C" { void showm(void); } int main (void) { showm(); return 0; } 

There is fort.f90

 subroutine showm print *, "fort " end subroutine showm 

I collect through cmake:

 cmake_minimum_required(VERSION 3.4) project(main) enable_language(CXX) enable_language (Fortran) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES main.cpp) set(FORTRAN_SOURCES fort.f90) add_library(F_OBJECTS_LIB ${FORTRAN_SOURCES}) add_executable(main ${SOURCE_FILES}) target_link_libraries(main F_OBJECTS_LIB) 

As a result, the linker gives an error:

/home/mk/clion-2016.1.1/bin/cmake/bin/cmake --build
/home/mk/.CLion2016.1/system/cmake/generated/proba-
c5f6dd60 / c5f6dd60 / Debug --target all - -j 2 [50%] Built target
F_OBJECTS_LIB Scanning dependencies of target main [75%] Building CXX
object CMakeFiles / main.dir / main.cpp.o [100%] Linking CXX executable
main CMakeFiles / main.dir / main.cpp.o: In function main ':
/ home / mk / Desktop / proba / main.cpp: 4: undefined reference to
showm () 'collect2: error: ld completed with return code
one

Please help me, what am I doing wrong.

  • one
    And the Fortran library you have gathered? Judging by the conclusion - no. - aleks.andr
  • Fortran Library gathered - Max

2 answers 2

For a unix build, it is right to describe a function like this:

 subroutine showm bind(C,name="showm") print *, "fort " end subroutine showm 

    Add the symbol _ to the name of the Fortran function:

     #include <stdio.h> extern "C" { void showm_(void); // showm -> showm_ } int main (void) { showm_(); // showm -> showm_ return 0; } 

    I don’t know why, but it works. I would be glad if someone explains.

    • aleks.andr, this only works on windows - Max
    • In unix, when declaring the Fortran function, you need to write subroutine showm bind (C, name = "showm") print *, "fort" end subroutine showm
    • if it's intel fortran, then its assembler names are controlled by options: large / small characters, underscore: front / rear / no. Because for example, VC ++ has a front line in x86, but does not have a x64 - Pavel Gridin