Undefined references issue

Consider the following tree structure:

├── app
│   └── CMakeLists.txt
├── lib
│   ├── bindings
│   │   ├── CMakeLists.txt
│   │   └── fortran
│   │       └── CMakeLists.txt
│   ├── CMakeLists.txt
│   └── src
│       ├── CMakeLists.txt
│       ├── core
│       │   ├── CMakeLists.txt
│       └── include
│           ├── CMakeLists.txt
├── CMakeLists.txt
# Top level CMakeLists.txt
project( project
   LANGUAGES C Fortran
)
add_subdirectory( lib )
add_subdirectory( app )

# lib/CMakeLists.txt
add_subdirectory( src )
add_subdirectory( bindings )

# lib/src/CMakeLists.txt
set( sources )
add_subdirectory( include )
add_subdirectory( core )
add_library( Lib STATIC ${sources} )

# lib/bindings/CMakeLists.txt
add_subdirectory( fortran )

# lib/bindings/fortran/CMakeLists.txt
add_library( LibF STATIC libf.F90 )
target_link_libraries( LibF PUBLIC Lib )

# app/CMakeLists.txt
add_executable( app app.F90 )
target_link_libraries( app PRIVATE LibF )

It consists of a main C library (lib/src), a Fortran (interface module) library (lib/bindings/fortran), and a Fortran application executable (app).
When linking the app target, I am getting plenty of undefined references errors to the C-library symbols. The strange thing is that I made a very toy reproducer, to see if I was able to narrow down the issue, but there I do not have any issues, so I do not really understand what’s wrong in this case.

I even checked the last linking command:

/usr/bin/gfortran CMakeFiles/app.dir/app.F90.o -o main  ../lib/bindings/fortran/LibF.a ../lib/src/Lib.a

which has the exact same order in the real case, which nonetheless fails…

How can I debug this further?

To debug it further it’s good to put minimal example on Github for review. It can be really small and dumb project (not real, but similar to your structure).

Just wild guess - try to enable fPIC with set(CMAKE_POSITION_INDEPENDENT_CODE ON) for your project. Not sure if this is helps, but you can try.

Also on top of CMakeLists.txt should be cmake_minimum_required

1 Like

Thanks for answering.
-fPIC did not help…
Indeed I had the minimum version requirement at the top.

I put a a toy project which tries to get as close as possible in terms of structure here.

One thing I notice is that not all symbols fail to be resolved.

PS: that toy project compiles and links smoothly, as it should be. So, that does not help me understanding where the problem is…

Ok, I found the issue.
When declaring C procedure interfaces in Fortran, you cannot mix implict/explicit name bindings.
Adding bind(c, name="c_name") to all interface procedures solved the issue.