MinGW/CMake Undefined Reference to ZLib - c++

I have a project I want to build with CMake and compile with MinGW. The project uses Zlib. When I build with CMake I get no errors but then when I run MinGW Make it gives the following output:
C:\Projects\MultiMCBuild>C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe
.
.
.
[ 50%] Linking CXX shared library ..\libMultiMC_logic.dll
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x1f6c): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x29e2): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2a6d): undefined reference to 'z_get_crc_table'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2ca7): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2f52): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2f77): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3239): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3317): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3626): undefined reference to 'z_crc32'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x371f): undefined reference to 'z_inflate'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x376a): undefined reference to 'z_crc32'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3a57): undefined reference to 'z_inflateEnd'
C:/Qt/Tools/mingw492_32/bin/../lib/gcc/i686-w64- mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj): bad reloc address 0x20 in section `.eh_frame'
collect2.exe: error: ld returned 1 exit status
logic\CMakeFiles\MultiMC_logic.dir\build.make:3186: recipe for target 'libMultiMC_logic.dll' failed
mingw32-make[2]: * * * [libMultiMC_logic.dll] Error 1
CMakeFiles\Makefile2:1806: recipe for target 'logic/CMakeFiles/MultiMC_logic.dir/all' failed
mingw32-make[1]: * * * [logic/CMakeFiles/MultiMC_logic.dir/all] Error 2
makefile:159: recipe for target 'all' failed
mingw32-make: * * * [all] Error 2
Anyone have a clue what I can do to fix this? I read that the code can't find the ZLib library, how do I link it?
EDIT here is my CMakeLists.txt. I got this from the Git project.

I just tried to generate a small example to reproduce your errors. I took my test file from the home page of zlib http://zlib.net/zpipe.c
My initial CMakeLists.txt was
cmake_minimum_required(VERSION 3.4)
project(zlib_test)
set(ZLIB_TEST_SOURCES zpipe.c)
add_executable(${PROJECT_NAME} ${ZLIB_TEST_SOURCES})
And I got the same errors
[ 50%] Building C object CMakeFiles/zlib_test.dir/zpipe.c.o
[100%] Linking C executable zlib_test
CMakeFiles/zlib_test.dir/zpipe.c.o: In function `def':
zpipe.c:(.text+0x65): undefined reference to `deflateInit_'
zpipe.c:(.text+0xcd): undefined reference to `deflateEnd'
zpipe.c:(.text+0x135): undefined reference to `deflate'
zpipe.c:(.text+0x1cf): undefined reference to `deflateEnd'
zpipe.c:(.text+0x25d): undefined reference to `deflateEnd'
CMakeFiles/zlib_test.dir/zpipe.c.o: In function `inf':
zpipe.c:(.text+0x2eb): undefined reference to `inflateInit_'
zpipe.c:(.text+0x353): undefined reference to `inflateEnd'
zpipe.c:(.text+0x3a4): undefined reference to `inflate'
zpipe.c:(.text+0x404): undefined reference to `inflateEnd'
zpipe.c:(.text+0x476): undefined reference to `inflateEnd'
zpipe.c:(.text+0x4a6): undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
make[2]: *** [zlib_test] Error 1
make[1]: *** [CMakeFiles/zlib_test.dir/all] Error 2
make: *** [all] Error 2
After changing CMakeLists.txt to this form
cmake_minimum_required(VERSION 3.4)
project(zlib_test)
find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
set(ZLIB_TEST_SOURCES zpipe.c)
add_executable(${PROJECT_NAME} ${ZLIB_TEST_SOURCES})
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
I could compile the program.
So your problem is: Where is ZLIB added in your CMakeLists.txt? At least, you need the line find_package(ZLIB REQUIRED).

Related

linked library is valid in CMakeLists, but doesn't link at compile time

I'm just starting around with messing around with vulkan, and GLFW, but when I try to compile a test program, it gives me a bunch of linker errors:
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Init()':
loops.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: loops.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x4f): undefined reference to `glfwCreateWindow'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Update()':
loops.cpp:(.text+0xa3): undefined reference to `glfwPollEvents'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::DeInit()':
loops.cpp:(.text+0xcd): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: loops.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vulkan_test.dir/build.make:113: vulkan_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/vulkan_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(vulkan_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH /home/headass/CMake_Modules/)
find_package(GLFW REQUIRED)
find_package(Vulkan REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS} ${VULKAN_INCLUDE_DIRS})
add_executable(vulkan_test main.cpp loops.cpp)
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
Any idea why this is happening? I have both vulkan and GLFW installed, libglfw.so is in my /usr/lib/ directory, and clangd doesn't see anything wrong with it, but it still fails to link properly.
And yes, I HAVE tried googling this, to no avail.
Changing:
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
to:
target_link_libraries(vulkan_test glfw ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
worked flawlessly

Undefined reference to bsoncxx when compiling mongodb-cxx-driver

os: Windows 10
compiler: MinGW w64 8.1.0 i686
mongo-c-driver: 1.16.2
mongo-cxx-driver: commit 4629521 of branch releases/v3.5
Compiled with the options:
BSONCXX_POLY_USE_STD=ON
CMAKE_CXX_STANDARD=17
I receive the following errors when try to compile mongo-cxx-driver:
[ 13%] Linking CXX executable test_bson.exe
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0xf17): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types10b_document7type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x10ae): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types7b_int327type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x1563): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types7b_array7type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x16f2): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types7b_int327type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x1b5d): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types7b_int327type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x1e42): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types6b_bool7type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x2292): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types7b_int327type_idE'
CMakeFiles\test_bson.dir/objects.a(bson_builder.cpp.obj):bson_builder.cpp:(.text+0x2579): undefined reference to `_imp___ZN7bsoncxx7v_noabi5types6b_bool7type_idE'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [src\bsoncxx\test\CMakeFiles\test_bson.dir\build.make:236: src/bsoncxx/test/test_bson.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:987: src/bsoncxx/test/CMakeFiles/test_bson.dir/all] Error 2
mingw32-make: *** [Makefile:160: all] Error 2
The answer is that the mongodb-cxx driver for MinGW is not supported: https://developer.mongodb.com/community/forums/t/undefined-reference-to-bsoncxx-when-try-to-compile/2896/4?u=alexxanderx

undefined reference error while installing OpenCV not compiling

I tried to find the same case as mine, but I couldn't. Most of them here are about undefined reference error during compiling their own code, but in my case this error occurred while installing OpenCV.
I referred to this page :
http://milq.github.io/install-opencv-ubuntu-debian/
I use armv7l and debian-scratch 9.3.
I tried to install OpenCV 3.4.0
I have this error :
[ 31%] Building CXX object modules/features2d/CMakeFiles/opencv_features2d.dir/src/akaze.cpp.o
//usr/lib/libgdal.so.20: undefined reference to `XML_SetUserData'
//usr/lib/libgdal.so.20: undefined reference to `XML_SetUnknownEncodingHandler'
//usr/lib/arm-linux-gnueabihf/libharfbuzz.so.0: undefined reference to `FT_Set_Char_Size'
//usr/lib/arm-linux-gnueabihf/libQt5Gui.so.5: undefined reference to `glGenerateMipmap'
//usr/lib/arm-linux-gnueabihf/libQt5Gui.so.5: undefined reference to `glUniform2fv'
//usr/lib/arm-linux-gnueabihf/libbluray.so.1: undefined reference to `FT_GlyphSlot_Embolden'
...
//usr/lib/arm-linux-gnueabihf/libavcodec.so.57: undefined reference to `deflateReset'
/usr/bin/ld: ../../bin/opencv_annotation: hidden symbol `atexit' in /usr/lib/arm-linux-gnueabihf/libc_nonshared.a(atexit.oS) is referenced by DSO
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
apps/annotation/CMakeFiles/opencv_annotation.dir/build.make:100: recipe for target 'bin/opencv_annotation' failed
make[2]: *** [bin/opencv_annotation] Error 1
CMakeFiles/Makefile2:4690: recipe for target 'apps/annotation/CMakeFiles/opencv_annotation.dir/all' failed
make[1]: *** [apps/annotation/CMakeFiles/opencv_annotation.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
I found out it's from a linking library error, but as I'm a beginner of Linux, it couldn't help me.
I hope this question is clear.

libx264.so.146 not found while compiling C++ code or x264 itself

I just upgraded my system from Ubuntu 15.10 to Ubuntu 16.04 LTS. Now, for some reason my C++ project won't compile anymore. CLion keeps saying it's missing 'libx264.so.146 as you can see in the compile log:
/home/lorre851/CLion/bin/cmake/bin/cmake --build /home/lorre851/.CLion12/system/cmake/generated/ce7171e4/ce7171e4/Release --target main -- -j 8
[ 20%] Linking CXX executable build/main
/usr/bin/ld: warning: libx264.so.146, needed by /usr/local/lib/libavcodec.so.57, not found (try using -rpath or -rpath-link)
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_encoder_open_146'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_encoder_close'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_bit_depth'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_encoder_headers'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_encoder_encode'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_encoder_reconfig'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_param_parse'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_param_apply_profile'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_param_apply_fastfirstpass'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_levels'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_picture_init'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_param_default'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_param_default_preset'
/usr/local/lib/libavcodec.so.57: undefined reference to `x264_encoder_delayed_frames'
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:172: recipe for target 'build/main' failed
make[3]: *** [build/main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[2]: *** [CMakeFiles/main.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/main.dir/rule' failed
make[1]: *** [CMakeFiles/main.dir/rule] Error 2
Makefile:118: recipe for target 'main' failed
make: *** [main] Error 2
Most people seem to give 'reinstalling x264' as a solution. However, reinstalling x264 with the following commands leads to the same error while compiling x264 itself.
git clone git://git.videolan.org/x264
cd x264
./configure --enable-static
make
/usr/bin/ld: warning: libx264.so.146, needed by /usr/local/lib/libavcodec.so, not found (try using -rpath or -rpath-link)
/usr/local/lib/libavcodec.so: undefined reference to `x264_encoder_open_146'
collect2: error: ld returned 1 exit status
Makefile:202: recipe for target 'x264' failed
make: *** [x264] Error 1
Does anyone have an idea how I could get around this?
Okay, after a day of trying to remove and recompile the liavcodec-dev library, without result, I decided to format the drive and reinstall Ubuntu from scratch.
Everything is working fine now.

CMake linker not working correctly

I am trying to build my project using CMake but I am having error linking required libraries. I have this CMakeLists.txt in the root folder of my project:
cmake_minimum_required(VERSION 2.6)
project(test)
add_subdirectory(src)
And in my src folder, alongside my source files I have this CMakeLists.txt:
set (CMAKE_CXX_FLAGS "Wall -std=c++11" )
set (CMAKE_EXE_LINKER_FLAGS "-lSDL2 -lGL" )
file (GLOB SRCS *.cpp *.h )
add_executable(engine ${SRCS} )
I then go into the build folder and do cmake .. and it runs without any errors. When I do make, the compilation runs without any errors as well, but when it gets to the linking part, I get these errors:
CMakeFiles/test.dir/Application.cpp.o: In function `Application::onExecute()':
Application.cpp:(.text+0x41): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0x4e): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0xd7): undefined reference to `SDL_PollEvent'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::render()':
Application.cpp:(.text+0x17b): undefined reference to `glClearColor'
Application.cpp:(.text+0x185): undefined reference to `glClear'
Application.cpp:(.text+0x194): undefined reference to `SDL_GL_SwapWindow'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::cleanUp()':
Application.cpp:(.text+0x1b2): undefined reference to `SDL_GL_DeleteContext'
Application.cpp:(.text+0x1c1): undefined reference to `SDL_DestroyWindow'
Application.cpp:(.text+0x1c6): undefined reference to `SDL_Quit'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::initialize()':
Application.cpp:(.text+0x1de): undefined reference to `SDL_Init'
Application.cpp:(.text+0x1ea): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x22b): undefined reference to `SDL_CreateWindow'
Application.cpp:(.text+0x243): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x25a): undefined reference to `SDL_Quit'
Application.cpp:(.text+0x270): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x27f): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x28e): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x29d): undefined reference to `SDL_GL_CreateContext'
Application.cpp:(.text+0x2af): undefined reference to `glGetString'
collect2: error: ld returned 1 exit status
make[2]: *** [src/test] Error 1
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2
I have the correct includes in my header files and I was able to compile and run using only make so I think the linker flags I tell CMake aren't being passed to the compiler. How can I fix this?
You have to use the cmake command target_link_libraries to reference SDL libs.