I'm trying to create a project, that will create an executable MyExec.exe, which will depends on another library Niftilib, which itself depends on zlib.
What I did is that I used cmake and created a superbuild, using ExternalProject_Add to download and compile zlib:
ExternalProject_Add(Zlib
SOURCE_DIR "${PROJECT_BINARY_DIR}/deps/zlib"
BINARY_DIR "${PROJECT_BINARY_DIR}/deps/zlib-build"
INSTALL_DIR "${PROJECT_BINARY_DIR}/deps/zlib-install"
GIT_REPOSITORY "${git_protocol}://github.com/madler/zlib.git"
GIT_TAG "50893291621658f355bc5b4d450a8d06a563053d"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DINSTALL_BIN_DIR=<INSTALL_DIR>/bin
-DINSTALL_INC_DIR=<INSTALL_DIR>/include
-DINSTALL_LIB_DIR=<INSTALL_DIR>/lib
-DINSTALL_MAN_DIR=<INSTALL_DIR>/share/man
-DINSTALL_PKGCONFIG_DIR=<INSTALL_DIR>/share/pkgconfig)
I did the same for Nifticlib:
ExternalProject_Add(Nifticlib
SOURCE_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib"
BINARY_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib-build"
INSTALL_DIR "${PROJECT_BINARY_DIR}/deps/nifticlib-install"
GIT_REPOSITORY "${git_protocol}://gitlab.com/slckr/nifticlib.git"
GIT_TAG "9f427b39eb6a16822c6ed34454f87eb751b23220"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DZLIB_INCLUDE_DIR=${ZLIB_INC_DIR}
-DZLIB_LIBRARY_DEBUG=${ZLIB_LIB_DIR}/zlibd.lib
DEPENDS Zlib)
Then I did the same for MyExec:
ExternalProject_Add(MyExec
SOURCE_DIR "${PROJECT_BINARY_DIR}/MyExec"
BINARY_DIR "${PROJECT_BINARY_DIR}/MyExec-build"
INSTALL_DIR "${PROJECT_BINARY_DIR}/MyExec-install"
GIT_REPOSITORY "${git_protocol}://gitlab.com/slckr/MyExec.git"
GIT_TAG "8514aacf5422f1111602e24ea73b229556293b8c"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DBOOST_INC_DIR=${Boost_INCLUDE_DIRS}
-DBOOST_LIB_DIR=${Boost_LIBRARY_DIRS}
-DZLIB_INC_DIR=${ZLIB_INC_DIR}
-DZLIB_LIB_DIR=${ZLIB_LIB_DIR}
-DNIFTI_INC_DIR=${NIFTI_INC_DIR}
-DNIFTI_LIB_DIR=${NIFTI_LIB_DIR}
DEPENDS Nifticlib)
And in my CMakeLists of MyExec project, I have this:
cmake_minimum_required(VERSION 3.6)
project(MyExec)
include_directories(${BOOST_INC_DIR} ${ZLIB_INC_DIR} ${NIFTI_INC_DIR})
link_directories(${BOOST_LIB_DIR} ${ZLIB_LIB_DIR} ${NIFTI_LIB_DIR})
add_subdirectory(DataStructure)
add_subdirectory(FileHandler)
add_subdirectory(Parser)
add_executable(${PROJECT_NAME} MyExec.cc)
target_link_libraries(${PROJECT_NAME} zlibd znz nifticdf niftiio DataStructure FileHandler Parser)
install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
My goal is to be able to distribute MyExec.exe without having all the required libraries (except boost). I want nifticlib library to be embedded in my exec.
If I compile this way, it doesn't work, because it says that zlibd.dll is missing. I find out that I had to link my executable to zlibstaticd instead of zlibd.
But my question is, should I also change -DZLIB_LIBRARY_DEBUG=${ZLIB_LIB_DIR}/zlibd.lib
to -DZLIB_LIBRARY_DEBUG=${ZLIB_LIB_DIR}/zlibstaticd.lib
in Nifticlib project ?
Or does it only matter in the executable project ? Why does nifticlib require me to provide the path to the zlib library, if it's build as a static library and doesn't do any linking ?
Thank you.
Edit: If I dont link the zlib to my executable, I get these errors:
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzdopen referenced in function znzdopen
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzread referenced in function znzread
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzwrite referenced in function znzwrite
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzputs referenced in function znzputs
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzgets referenced in function znzgets
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzputc referenced in function znzputc
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzgetc referenced in function znzgetc
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzflush referenced in function znzflush
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzeof referenced in function znzeof
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzclose referenced in function Xznzclose
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzopen referenced in function znzopen
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gzseek referenced in function znzseek
5>znz.lib(znzlib.obj) : error LNK2019: unresolved external symbol gztell referenced in function znztell
znz is part of nifticlib. It is the part using the zlib library.
Edit 2: To make it simpler, my question is does the linking happen like Linking 1 or Linking 2 (knowing that nifticlib depends on zlib, both compiled as static libraries):
Linking 1 Linking 2
-------------- -------------- ---------------
| zlib | | zlib | | nifticlib |
-------------- -------------- ---------------
| \ /
-------------- \ /
| nifticlib | \ /
-------------- \ /
| -------------------
-------------- | MyExec.exe |
| MyExec.exe | -------------------
--------------
The usual way is that linking is only done when an executable binary is generated. This includes for example normal executables, but also dynamic libraries (.dll, .so). Linking is not done for static libraries (.lib, .a).
That means if you build a static library of "nifticlib" it does not need to be linked to "zlib".
You should definitly change the dependency from the dynamic one to the static one, because CMake carries dependencies from static libraries over to the final binary. In this case it should not matter though, because you don't specify a real CMake dependency, just some variable for the library.
Note: In Visual Studio you can pretty easily "link" static libraries into each other. What it does is just concatenation of all functions into one .lib file. CMake does not use this feature though and instead carries dependencies over like mentioned above when doing "TARGET_LINK_LIBRARIES" on a static library target.