cmakeshared-librariesldmingw-w64nifti

Buidling NIfTI C library in Windows: undefined references?


I'm building the NIfTI C libraries on Windows using MinGW gcc 14.2.0 and CMake 3.30.3. I have noticed two things:

1: I had previously compiled the Z library, libz.dll.a with the same build tools, and although the path to the library was found automatically and I had set the include directory that holds zlib.h, compilation of the "znz" library required to open both .nii and .nii.gz files fails. I can make that work by also adding the directory to the CMAKE_C_FLAGS variable (and just to make sure, also to CMAKE_CXX_FLAGS).
2: Now I can compile the static libraries, including the executables etc., but when I check the BUILD_SHARED_LIBS flag to build DLLs, after successfully building libznz.dll, libnifticdf.dl, libniftiio.dll, second_test.exe, nifti_stats.exe, nifti1_test.exe building first_test.exe fails with the errors:

ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c (.text.startup+0x27c): undefined reference to `nifti_intent_code'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x2f8): undefined reference to `nifti_stat2hzscore'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x34f): undefined reference to `nifti_stat2cdf'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x40a): undefined reference to `nifti_stat2rcdf'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x45a): undefined reference to `nifti_cdf2stat'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x508): undefined reference to `nifti_stat2zscore'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x5e7): undefined reference to `nifti_stat2cdf'
ld.exe: CMakeFiles\nifti_stats.dir/objects.a(nifti_stats.c.obj):nifti_stats.c:(.text.startup+0x607): undefined reference to `nifti_stat2cdf'
collect2.exe: error: ld returned 1 exit status

Does anyone understand


Solution

  • This change in the CMakeLists.txt files of the library worked for me in the end:

    In `Testing\niftilibCMakeLists.txt` it says

    ADD_EXECUTABLE(first_test nifti_test.c)
    TARGET_LINK_LIBRARIES(first_test niftiio)
    

    After changing the 2nd line to

    TARGET_LINK_LIBRARIES(first_test niftiio nifticdf)
    

    everything was built successfully.

    In what I understand of CMake's target_link_libraries behaviour, if niftiio links nifticdf or vice versa, it is enough to include the top library. The fact that it is not must mean they're independent.