Cmake not linking libraries that are found
Hello
I have a small C project in Clion I have running fine in Linux that I'm trying to get running in Windows (Msys2)
The trouble is that even though Cmake finds my external library, it fails to link it with target_link_libraries.
Here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(gen_utility C)
add_executable(gen_utility main.c )
add_library(grid grid_sheet.c)
target_include_directories(gen_utility
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}
PUBLIC ${CMAKE_SOURCE_DIR}/include
)
find_library(
HPDF_LIB
NAMES libhpdf hpdf
HINTS "${CMAKE_SOURCE_DIR}/lib"
)
message(STATUS "HPDF_LIB: [${HPDF_LIB}]")
target_link_libraries(grid ${HPDF_LIB})
target_link_libraries(gen_utility ${HPDF_LIB} grid)
Which results in:
"C:\Program Files\JetBrains\CLion 2018.2.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProjects\gen_utility\cmake-build-debug --target all -- -j 2
[ 50%] Built target grid
[ 75%] Linking C executable gen_utility.exe
CMakeFiles\gen_utility.dir/objects.a(main.c.obj): In function `show_stripe_pattern':
C:/Users/User/CLionProjects/gen_utility/main.c:33: undefined reference to `HPDF_Page_SetRGBStroke'
C:/Users/User/CLionProjects/gen_utility/main.c:34: undefined reference to `HPDF_Page_SetLineWidth'
C:/Users/User/CLionProjects/gen_utility/main.c:35: undefined reference to `HPDF_Page_MoveTo'
C:/Users/User/CLionProjects/gen_utility/main.c:36: undefined reference to `HPDF_Page_TextWidth'
...
But I'm confused because Cmake seems to find the library:
C:\Program Files\JetBrains\CLion 2018.2.3\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\User\CLionProjects\gen_utility
-- HPDF_LIB: [C:/Users/User/CLionProjects/gen_utility/lib/libhpdf.a]
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/User/CLionProjects/gen_utility/cmake-build-debug
[Finished]
Any ideas?
Please sign in to leave a comment.
Hi @Dbeauchesne. Have you tried to rebuild libhpdf.a on Windows also?
Hi Maxim. Yes that's what I did initially.
Have you see errors in the CLion editor in main.c:33 file or it looks ok?
I think I figured this out-- I was using a different GCC to build the library as the one I had defaulted to in Clion.
That said, now I'm getting this:
[ 75%] Linking C executable gen_utility.exe
../lib/libhpdf.a(hpdf_image_ccitt.o):(.text+0x5aa): undefined reference to `__assert_func'
../lib/libhpdf.a(hpdf_streams.o):(.text+0x1201): undefined reference to `__errno'
../lib/libhpdf.a(hpdf_streams.o):(.text+0x12b1): undefined reference to `__errno'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [gen_utility.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/gen_utility.dir/all] Error 2
CMakeFiles\gen_utility.dir\build.make:88: recipe for target 'gen_utility.exe' failed
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/gen_utility.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed
So I'm not sure how to go about fixing libc linking issues here.
How did you get these errors? Did you rebuild the library with another gcc? Or did you try to use gcc that was used for building the library in CLion?