CMake: wrong zlib found - how to build zlib from src (with main project CMakeLists.txt) and link it? - visual-studio-2010

// brief version
How can I make CMake to use my supplied zlib (which it also has to build from source) instead of the one found by the finder without breaking the finder for other libs (OpenGL)?
ZLib needs to be used by the main project and also libPNG which comes as source as well.
Primary target platform is Windows.
// longer version:
In my project I need to link against libpng, zlib and OpenGL. With libpng being dependent on zlib. But zlib is also required by the main project.
I need to supply sourcecode for all libs except OpenGL, and build those libraries along with
the main project to assert linking the correct version and simplify building on Windows.
I found ways to do all this with custom libraries where no built-in finder exists, but I can't override the finder properly for just zlib. If I change the search path for libs, then OpenGL is not found.
However I can't get cmake to use my supplied zlib instead of a rouge zlib.DLL that the package finder finds somewhere in my system. (The one from tortoise git)
I tried to set ZLIB_LIBRARY to a specific filepath, but that only works on MinGW, and I also think this is not the way to do it.
(And also I had to explicitly link to png16_static instead of just libpng, for an inexplicable reason.)
Any help on this is much appreciated. Maybe I'm taking this on the wrong way?
Target&Development Platform:
Windows7
Visual Studio 2010
and MinGW (both need to work)
My (simplified example) CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (MyProject)
find_package(OpenGL)
add_executable(MyProject main.cpp)
include_directories(${INCLUDE_DIRECTORIES} "${PROJECT_BINARY_DIR}")
include_directories(${INCLUDE_DIRECTORIES} "external_libs/lpng162")
include_directories(${INCLUDE_DIRECTORIES} "external_libs/zlib-1.2.8")
include_directories(${INCLUDE_DIRECTORIES} "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8")
add_subdirectory("external_libs/zlib-1.2.8")
link_directories(${LINK_DIRECTORIES} "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8")
# libpng will not build correctly if this not set
set (ZLIB_ROOT "${PROJECT_SOURCE_DIR}/external_libs/zlib-1.2.8")
# manually set this to prevent cmake from finding the tortiose-git zlib.dll first
# DOES NOT WORK CORRECTLY, only with mingw32
set (ZLIB_LIBRARY "${PROJECT_BINARY_DIR}/external_libs/zlib-1.2.8/libzlib.dll")
add_subdirectory("external_libs/lpng162")
TARGET_LINK_LIBRARIES(MyProject png16_static zlib ${OPENGL_LIBRARY})
Project (simplified example) structure:
./main.cpp
./CMakeLists.txt
./external_libs/zlib-1.2.8/ <- contains respective source
./external_libs/lpng162/ <- contains respective source

Third-party libraries most likely call FindZLIB.cmake to determine the location of CMake. You already had the right idea by setting the ZLIB_LIBRARY manually, but were not quite getting it right:
add_subdirectory(<path_to_zlib_src_dir>)
set(ZLIB_INCLUDE_DIR "<path_to_zlib_src_dir>" "${CMAKE_BINARY_DIR}/<path_to_zlib_build_dir>")
set(ZLIB_LIBRARY zlib)
add_subdirectory(<path_to_lpng_src_dir>)
The include directory needs to contain both src and build path as zconf.h is build by CMake
The library name is only the CMake-target name, not the complete path to the resulting file.
On Windows dlls are not automatically copied by CMake. You might want to add some additional code to make sure that the zlib and lpng dlls end up in the right place.
You can call find_package(zlib) yourself to make sure it behaves as expected
In the rare case that a third-party lib does not use the find script, you will have to dig into that project's CMakeLists to find out what is going on

Related

Building a cmake project which uses boost with a local distribution

I'm trying to build kenlm which uses boost. The version of boost already installed on the system seems incompatible or too old or broken.
I have successfully built a copy of boost in my ~/local directory. I would like to tell cmake to use the boost headers found under ~/local/include and the libraries in ~/local/lib instead. However I can't see how to tell cmake to look at my custom path for libraries or headers. I've tried setting LD_LIBRARY_PATH to ~/local/lib:$LD_LIBRARY_PATH. Is there an equivalent variable for include path as well? I haven't been able to find one.
If there is no way to do this with just environment flags, I've been trying to edit the CMakeLists.txt file but haven't had much success. I tried editing in my include path but it still uses whatever find_package(Boost ..) finds which is the /usr/include header.

compiling SFML with cmake and mingw under windows

When i try to run cmake to build a project where i include the SFML library i get the following Error: SFML found but version too low (requested: 2.4, found: 1.x.x)
I downloaded only the source of the newest Version of the library (SFML-2.4.2). I than run cmake (with MinGW Makefiles) And build the binaries into the same folder.
I copied the FindSFML into an subfolder of my project.
After that i had the following folder structure
SFML-2.4.2\
cmake\
Modules\
FindSFML.cmake
CMakeFiles
doc
...
include
lib
src
..
sfml-games\
tetris\
cmake_modules\
FindSFML.cmake
CMakeLists.txt
main.cpp
tetris-build
...
My CMakeLists.txt contains the following stuff:
project(Tetris)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake_modules")
Find_package(SFML 2 REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${SFML_LIBRARY})
I found that in cmake the entry CMAKE_INSTALL_PREFIX (which i dont get) is set to C:\Program Files (x86)\SFML so i put the library and the lib of SFML into C:\Program Files (x86)\SFML. (what is that CMAKE_INSTALL_PREFIX and should i realy always copy the library and lib folders into my C:\Program Files (x86)\ ??). Now my cmake runs through but when i try to compile the code i get a lot of undifined references to '_imp__....'
By the way on linux i just installed SFML via sudo apt-get install libsfml-dev and it works out of the box.
You're confusing things. CMAKE_INSTALL_PREFIX is the default location where to put SFML when you build the install target (i.e. running make install). This has nothing to do with your other issues.
You don't have to install SFML (or any other library) somewhere under C:\Program Files. That's completely up to you.
What I found to be rather neat is installing MinGW to C:\usr (or creating a symlink to your installation folder) and also use that path for CMAKE_INSTALL_PREFIX when building SFML.
This way MinGW should behave pretty much the way you're used to from Linux (i.e. not having to specify paths for include dir or libraries etc.).
As for your undefined reference errors, you should create a new question only asking for these (and then include at least a few of them), as they have nothing to do with the installation directory.
Right now I can only guess, but it's most likely due to you using the wrong CMake variables for the linker. You won't notice this on Linux, since SFML will be in the default search path (which will also happen if you use C:\usr as described above).
To try fixing this, use this line:
target_link_libraries(${PROJECT_NAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})

How do I install uuid boost library?

I followed many tutorials to get boost installed, firstyly I downloaded boost and added path with:
path=%path%;
c:\program files (x86)\code block\mingw\bin
secondly I ran
bootstrap.bat gcc
and then
b2 toolset=gcc
but there is no such lib installed as UUID which I really need to my project. Is there any way to make it happen as I haven't found any specific help among all those topics and I'm getting really confused. I'm working on win10 if that makes any difference.
Did you read documentation?
Boost UUID library is header-only library. See Configuration:
The library does not require building or any special configuration to be used.
So you have to download boost library and unpack it to some place in your disk (for example to c:\boost). Path to boost library will be c:\boost\boost_1_62_0. Then you have to specify the compiler the place where the boost library is located. It is compiler or IDE specific way. But the common way is to use "Additional Include Directories" option of compiler. It is -I for GCC and /I for MS VC++. Last step is to write include directive in your c++ code. For example:
#include <boost/uuid/uuid.hpp>
For code example see Example and files in boost/libs/uuid/test/ folder

Building libraries that have ExternalProjects Added using CMake and Xcode

I am trying to use CMake to build a library (which in turn links to other libraries built via CMake). I am having a couple of issues with this process and would love to have some guidance.
For the library, I have a CMakeLists.txt which essentially has
set(SRC srcfile1.cpp srcfile2.cpp)
set(HEADERS srcfile1.h srcfile2.h)
add_library(myLib ${SRC} ${HEADERS})
INSTALL(FILES ${HEADERS} DESTINATION “include/myLib”)
For a “Unix Makefiles” generator, with cmake command
cmake -DCMAKE_INSTALL_PREFIX=. -DCMAKE_BUILD_TYPE=debug -G “Unix Makefiles” ..
I get an appropriate debug library libMyLib.a in build/src. For a release build type, a release build of libMyLib.a is placed in build/src
However, for an Xcode generator, the library is placed in src/Debug. The issues I am facing here are
a. Doing an Archive in Xcode doesn’t seem to have any effect. I don’t see a corresponding Release library being created anywhere
b. I also have a myLibTests target (which uses googles gtest) which is defined in it’s CMakeLists.txt as
set(SRC myTest.cpp)
add_executable(myLibTests ${SRC})
add_dependencies(myLibTests myLib gtest)
set(gTestLib External/src/gTestLib)
target_link_libraries(myLibTests myLib gTestLib)
When the cmake generator is “Unix Makefiles” myLibTests build, links and runs fine. gTestLib is placed in External/src/gtest-build/. But, when it is “Xcode”, it can’t find the gTest libraries. because, the library is placed in External/src/gTestLib/Debug(or Release) as the case may be (and the above path set in CMakeLists.txt is no longer valid). I am not sure how to set it’s path appropriately in the above set statement.
Since, debug/release is controlled in Xcode (and the configure process in CMake is unaware of this), I assume there isn’t a way to separate the release/debug builds of the gTestLib and also have CMake configure Xcode to pick up the appropriate version while building myLib i.e if I do a debug build of myLib it also does a debug build of gTest and links to it (similar for release builds)? Alternatively, is it possible to configure Xcode to place both the Debug and Release builds in the same location (and then hardcode it’s path above)? What would be the best way of configuring this?
Thanks

Why doesn't Xcode recognize my LIBRARY_SEARCH_PATHS?

I've set LIBRARY_SEARCH_PATHS to /opt/local/lib, and verified that the library in question is there (I'm linking to GLEW):
$ls /opt/local/lib
libGLEW.1.5.1.dylib libfreetype.a libz.a
libGLEW.1.5.dylib libfreetype.dylib libz.dylib
libGLEW.a libfreetype.la pkgconfig
libGLEW.dylib libz.1.2.3.dylib
libfreetype.6.dylib libz.1.dylib
but Xcode gives me the linker error
library not found for -lGLEW
I'm generating the Xcode project with CMake, so I don't want to explicitly modify the Xcode project (if someone suggests adding it as a framework, or something like that). Xcode recognizes USER_HEADER_SEARCH_PATHS fine (as in this question); why doesn't it work here?
Perhaps adding something like this to your CMakeLists.txt?
find_library(GLEW_LIB GLEW /opt/local/lib)
if(NOT ${GLEW_LIB})
message(FATAL_ERROR "Could not find GLEW")
endif()
target_link_libraries(myprogram ${GLEW_LIB} ...)
Where myprogram is the name of the target executable that needs to link with the library. You would replace the ... with the other libraries you are using on that executable.
This way CMake would handle the library path details for you.
Xcode works on potentially multiple SDK's, so whenever your define these kinds of things (like HEADER_SEARCH_PATHS or LIBRARY_SEARCH_PATHS) the current SDK root is prepended to the actual path that's getting passed to the linker.
So, one way to make this work would be to add your directory to the SDK. For example, assuming you're building with the Mac OS X 10.5 sdk, you could add your opt dir:
ln -s /opt /Developer/SDKs/MacOSX10.5.sdk/opt
Your library would now be found on your system.
If you don't want to do this, then you will have to look at CMake and find out how to get it to generate a library requirement for your actual library (I don't know anything about CMake, so I can't help you there). This is also why you see a difference between USER_HEADER_SEARCH_PATHS and HEADER_SEARCH_PATHS re your other question.
As another option, you could also specify this path with the OTHER_LDFLAGS build variable:
OTHER_LDFLAGS=-L/opt/local/lib
This would cause the linker to search /opt/local/lib as well as its standard paths and wouldn't require you to generate a different project file.

Resources