How to update the location of installed library on windows? - c++

I previously installed zlib library by "cmake ..", "make" and "make install". By calling function "find_package(zlib)" in CMakeLists.txt, the default installed location is found to be "C:\Program Files (x86)\zlib" . The content of CMakeLists.txt is written as:
cmake_minimum_required(VERSION 2.4.4)
project(zlib C)
find_package(ZLIB)
message(STATUS "Found")
message(STATUS " ${ZLIB_INCLUDE_DIRS}")
Now I reinstall this library by "cmake -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX:PATH=D:/TEST_ZLIB/ ..", getting the result as bellow
E:\Code\cpp\zlib-1.2.11\build>make install
[ 41%] Built target zlib
[ 46%] Built target minigzip64
[ 85%] Built target zlibstatic
[ 90%] Built target example
[ 95%] Built target minigzip
[100%] Built target example64
Install the project...
-- Install configuration: ""
-- Installing: D:/TEST_ZLIB/lib/libzlib.dll.a
-- Installing: D:/TEST_ZLIB/bin/libzlib.dll
-- Installing: D:/TEST_ZLIB/lib/libzlibstatic.a
-- Installing: D:/TEST_ZLIB/include/zconf.h
-- Installing: D:/TEST_ZLIB/include/zlib.h
-- Installing: D:/TEST_ZLIB/share/man/man3/zlib.3
-- Installing: D:/TEST_ZLIB/share/pkgconfig/zlib.pc
However, function find_package(zlib) still reports the previous location:
E:\Code\cpp\test_cmake\build>cmake ..
-- Found
-- C:/Program Files (x86)/zlib/include
-- Configuring done
-- Generating done
-- Build files have been written to: E:/Code/cpp/test_cmake/build
How can I update the location of this library? Any help would be deeply appreciated.

Result of find_package() call is cached, so running it the second time doesn't search actually, but uses the cached value.
CMake cache is contained in CMakeCache.txt file located in the binary directory. So you have 3 possibilities for force find_package to search again:
Remove the whole binary directory.
Next invocation of cmake will be "fresh": it will search for compiler, all needed packages, and so on.
Remove CMakeCache.txt file from the binary directory.
Next invocation of cmake will be "fresh", same as in the first case.
From CMakeCache.txt remove the lines, corresponded to your package.
E.g., for force find_package(ZLIB) to search again, you may remove all entries contained substring "zlib" (case-insensitive).
Next invocation of cmake will use cached settings for compiler, other packages, but search ZLIB again.
The 3d variant requires slightly more actions, but it will result with faster cmake call.
The 1st and 2nd variants are easy to perform and their effects are almost identical. They both result in "slow" cmake call, which would need to perform all checks again. But you may be sure that you won't leave cached setting in inconsistent case.
When troubleshooting, always choose 1st or 2nd variants!

Related

cmake fixup_bundle is not copying dll files

I am facing a curious error with VS15 (2017). It is a cmake project for c++ application.
The fixup_bundle is not working anymore and i don't know how to investigate the issue.
I am expecting the fixup_bundle to copy the necessary dll (from linked projects dirs) to the final bin folder.
Build succeeded.
[0/1] Install the project...
-- Install configuration: "Debug"
-- fixup_bundle
-- app='C:/Users/kat/CMakeBuilds/acb04e27-073b-0533-8428-dc0a45a40673/build/x64-Debug/bin/comp_service.exe'
-- libs=''
-- dirs='D:/DEV/_cpp/conan/p_lib/0.2.0/pxd/stable/package/6ee38d1eb1997983ad3a4abc75bb584cb379895f/bin;D:/DEV/_cpp/conan/p_legacy_lib/0.2.0/pxd/stable/package/3181232a4bdc0bd2668dc369a762dde5242d17f4/bin'
-- ignoreItems=''
-- fixup_bundle: preparing...
-- warning: could not find 'objdump' - cannot analyze prerequisites...
-- warning: could not find 'objdump' - cannot analyze prerequisites...
-- fixup_bundle: copying...
-- 1/4: *NOT* copying 'C:/Users/kat/CMakeBuilds/acb04e27-073b-0533-8428-dc0a45a40673/build/x64-Debug/bin/comp_service.exe'
-- 2/4: *NOT* copying 'C:/Userskat/CMakeBuilds/acb04e27-073b-0533-8428-dc0a45a40673/build/x64-Debug/bin/ut_comp_service.exe'
-- fixup_bundle: fixing...
-- 3/4: fix-up not required on this platform 'C:/Users/kat/CMakeBuilds/acb04e27-073b-0533-8428-dc0a45a40673/build/x64-Debug/bin/comp_service.exe'
-- 4/4: fix-up not required on this platform 'C:/Users/kat/CMakeBuilds/acb04e27-073b-0533-8428-dc0a45a40673/build/x64-Debug/bin/ut_comp_service.exe'
-- fixup_bundle: cleaning up...
-- fixup_bundle: verifying...
Any hints ?
Thanks for your help.
K.

Use CMake to build dependency (oneTBB as git submodule) as dynamic library?

I have a project that depends on Intel's oneTBB. My project is structured as follows:
external/
| - CMakeLists.txt
| - oneTBB/ (this is a git submodule)
| - ...
include/
lib/
include/
CMakeLists.txt
I currently get things to compile by manually building oneTBB and installing it inside a prefix directory located at external/oneTBB/prefix by running the following (bash) commands:
cd oneTBB
mkdir -p prefix
mkdir -p build
cd build
cmake -DCMAKE_INSTALL_PREFIX=../prefix -DTBB_TEST=OFF ..
cmake --build .
cmake --install .
I then simply include and link using this prefix. (I got this from following the oneTBB READMEs)
While this works without issue, I'm currently trying to clean up my CMake such that its easier to build on Windows as well. Ideally, I'm looking to get to a point where I can simply run:
mkdir build
cd build
cmake ..
cmake --build .
and my project will build itself and all dependencies.
I got this working with other dependencies such as glfw and eigen by simply adding (to the CMakeLists.txt in external/:
add_subdirectory(glfw)
add_subdirectory(eigen)
But adding add_subdirectory(oneTBB) throws a LOT of warnings, starting with:
CMake Warning at external/oneTBB/CMakeLists.txt:116 (message):
You are building oneTBB as a static library. This is highly discouraged
and such configuration is not supported. Consider building a dynamic
library to avoid unforeseen issues.
-- TBBBind build targets are disabled due to unsupported environment
-- Configuring done
CMake Warning (dev) at external/oneTBB/src/tbb/CMakeLists.txt:15 (add_library):
Policy CMP0069 is not set: INTERPROCEDURAL_OPTIMIZATION is enforced when
enabled. Run "cmake --help-policy CMP0069" for policy details. Use the
cmake_policy command to set the policy and suppress this warning.
INTERPROCEDURAL_OPTIMIZATION property will be ignored for target 'tbb'.
This warning is for project developers. Use -Wno-dev to suppress it.
I have no need to build oneTBB as a static library.
Am I doing something wrong in my attempt? Really all I need is for oneTBB to be built as a dynamic library and placed somewhere I can link it to (without installing it on the system overall)
Similar question:
I am also including the METIS library which depends on GKlib. Currently I'm doing this in a similar way to what I did for oneTBB where I manually build each using the following script:
# Setup the GKlib library:
cd GKlib
mkdir -p prefix
mkdir -p build
cd build
cmake -DCMAKE_INSTALL_PREFIX=../prefix ..
cmake --build . -j
cmake --install .
cd ../../
# Setup the METIS library:
cd METIS
mkdir -p prefix
make config prefix=../prefix gklib_path=../GKlib/prefix #(GKLib path is done from root, install path done relative to build)
make install -j
cd ../
When I try to add them using:
add_subdirectory(GKlib)
add_subdirectory(METIS)
it throws errors that METIS cannot find GKlib:
CMake Error at external/METIS/CMakeLists.txt:50 (add_subdirectory):
add_subdirectory given source "build/xinclude" which is not an existing
directory.
While I recognize this is a separate issue, I figured to include it here as it is related to my issues with add_subdirectory()
Many projects expect that you invoke CMake on them separately instead of adding them into an existing project with add_subdirectory. While there might be a way to make add_subdirectory work with oneTBB, there is an easier way.
CMake has a function that allows you to download, build, and install external projects at build time: ExternalProject_Add.
Here's an example for spdlog, taken straight from one of my own projects:
# project_root/thirdparty/spdlog/CMakeLists.txt
string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)
ExternalProject_Add(spdlog-project
GIT_REPOSITORY https://github.com/gabime/spdlog
GIT_TAG edc51df1bdad8667b628999394a1e7c4dc6f3658
GIT_SUBMODULES_RECURSE ON
GIT_REMOTE_UPDATE_STRATEGY CHECKOUT
INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/install"
LIST_SEPARATOR |
CMAKE_CACHE_ARGS
"-DCMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_CXX_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_EXE_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}:STRING=${CMAKE_SHARED_LINKER_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}"
"-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}"
"-DCMAKE_INSTALL_PREFIX:STRING=<INSTALL_DIR>"
"-DSPDLOG_BUILD_EXAMPLE:BOOL=OFF"
)
add_library(ext-spdlog INTERFACE)
add_dependencies(ext-spdlog spdlog-project)
ExternalProject_Get_property(spdlog-project INSTALL_DIR)
target_include_directories(ext-spdlog SYSTEM INTERFACE "${INSTALL_DIR}/include")
target_link_directories(ext-spdlog INTERFACE "${INSTALL_DIR}/lib")
target_link_libraries(ext-spdlog INTERFACE spdlog$<$<CONFIG:Debug>:d>)
After that, my project simply links against the created library target:
target_link_libraries(my_project ext-spdlog)
To adapt this for oneTBB, you have to switch out the repository URL and commit hash, and add your own CMake definitions (i.e. "-DTBB_TEST=OFF"). Depending on how oneTBB has its include and library directories set up, you may also have to change the target_include_directories and/or target_link_directories lines. I haven't looked this up yet, but I'm sure you can figure it out.
This works regardless of whether the external project is built as a static or shared library. You shouldn't use git submodules, though - instead, let CMake do the downloading. (It'll only download and build once; subsequent builds will not re-build the external project if it's already built and up-to-date.)
I have no need to build oneTBB as a static library. Am I doing something wrong in my attempt? Really all I need is for oneTBB to be built as a dynamic library and placed somewhere I can link it to (without installing it on the system overall)
All your diagnostic messages indicate that it's actually being configured to be built as a static library, and additional clues point to the probability that you've set BUILD_SHARED_LIBS to false in the scope where you add_subdirectory(oneTBB).
CMake Warning at external/oneTBB/CMakeLists.txt:116 (message):
You are building oneTBB as a static library. This is highly discouraged
and such configuration is not supported. Consider building a dynamic
library to avoid unforeseen issues.
If you look in oneTBB's CMakeLists.txt file, you'll the following:
if (NOT DEFINED BUILD_SHARED_LIBS)
set(BUILD_SHARED_LIBS ON)
endif()
if (NOT BUILD_SHARED_LIBS)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
message(WARNING "You are building oneTBB as a static library. This is highly discouraged and such configuration is not supported. Consider building a dynamic library to avoid unforeseen issues.")
endif()
And then right after that, you get
-- TBBBind build targets are disabled due to unsupported environment
The corresponding section of oneTBB's CMakeLists.txt file is:
if (TBB_FIND_PACKAGE OR TBB_DIR)
...
else()
if (APPLE OR NOT BUILD_SHARED_LIBS)
message(STATUS "TBBBind build targets are disabled due to unsupported environment")
else()
add_subdirectory(src/tbbbind)
endif()
...
Both of these clues indicate that in the variable scope at which you add_subdirectory(oneTBB), BUILD_SHARED_LIBS is set to a falsy value.
Set BUILD_SHARED_LIBS it to a truthy value (Ex. 1, TRUE, YES, ON, etc.) before doing add_subdirectory(oneTBB) and then restore the previous value afterward.
Ex.
set(BUILD_SHARED_LIBS_TEMP "${BUILD_SHARED_LIBS}")
set(BUILD_SHARED_LIBS YES)
add_subdirectory(oneTBB)
set(BUILD_SHARED_LIBS "${BUILD_SHARED_LIBS_TEMP}")
unset(BUILD_SHARED_LIBS_TEMP)

Detecting and Writing if ALL_BUILD was run from CMake

I am creating a project that is built using CMake requires some special packaging afterwards. I would like to ensure that the build is release and that every part has been built in release. I wrote the build type using the following code:
FUNCTION ( GetCMakeBuildType ret)
# Returns the generator string to get the current build type:
SET(${ret} "$<$<CONFIG:MinSizeRel>:MinSizeRel>$<$<CONFIG:Debug>:Debug>$<$<CONFIG:Release>:Release>$<$<CONFIG:RelWithDebInfo>:RelWithDebInfo>" PARENT_SCOPE)
ENDFUNCTION()
FUNCTION ( WriteBuildType )
GetCMakeBuildType(buildTypeString)
ADD_CUSTOM_TARGET(RecordBuildType ALL COMMAND echo ${buildTypeString} > ./target/build_type.txt)
ENDFUNCTION()
But I would also like to detect if ALL the libraries were built by determining if ALL_BUILD was the target or if they only built a single library while the rest is in debug. Can I detect if the user ran ALL_BUILD and write a flag to file to note that packaging is okay?

How to compile and run a hello world JUCE program?

I cloned https://github.com/juce-framework/JUCE
I first uncommented find_package(JUCE) in /GuiApp' CMakeList.txt.
Then I ran cmake .. . -B cmake-build-dir -D JUCE_BUILD_EXAMPLES=ON
on the top level directory.
It says:
-- Checking for modules 'webkit2gtk-4.0;gtk+-x11-3.0'
-- No package 'webkit2gtk-4.0' found
-- No package 'gtk+-x11-3.0' found
-- Checking for module 'alsa'
-- No package 'alsa' found
-- Configuring juceaide
-- Building juceaide
-- Exporting juceaide
CMake Error at examples/CMake/GuiApp/CMakeLists.txt:27 (find_package):
Could not find a package configuration file provided by "JUCE" with any of
the following names:
JUCEConfig.cmake
juce-config.cmake
Add the installation prefix of "JUCE" to CMAKE_PREFIX_PATH or set
"JUCE_DIR" to a directory containing one of the above files. If "JUCE"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
I tried to find where is JUCEConfig.cmake
/work/juce/temp/JUCE/build$ find . -name JUCEConfig.cmake
./cmake-build-dir/tools/JUCEConfig.cmake
How am I supposed to put this path in CMAKE_PREFIX_PATHS?
Clone this repository: https://github.com/juce-framework/JUCE
alsa and webkit2gtk are required dependencies which were absent on Ubuntu 18.04 in my computer. You too may have to install as follows:
sudo apt install libwebkit2gtk-4.0-dev libasound2-dev
In the examples/CMake folder in this repository there is a folder called GuiApp.
To build a new GUI App project with JUCE/CMake, do this (starting from scratch):
Copy the GuiApp folder to a new location.
In the GuiApp's CMakeLists.txt that you copied, replace the
add_subdirectory(JUCE)
with
add_subdirectory("<path_to_JUCE_cloned_repository>" JUCE)
replacing the path with the real location of the JUCE repository on your system.
From the GuiApp folder that you copied, run
cmake . -B cmake-build-dir
Above command will create a build tree folder named cmake-build-dir
cmake --build cmake-build-dir
Above command will build all targets in the cmake-build-dir folder.
After successful build, you can run the executable by going in directory:
cmake-build-dir/GuiAppExample_artefacts/
and issuing ./Gui\ App\ Example. That's the name of the resultant default executable.
Credit: https://forum.juce.com/u/reuk/summary

cmake: You have called ADD_LIBRARY for library cryptopp without any source files

Windows 64bit using cmake to compile c++ project: github.com/iHateInventNames/synergy-through-usb
I fixed a previous issue in this post: Compile issues: LIBUSB_1 with cmake project on Windows
So when I hit cmake now I get the following error:
> cmake .
-- Found libusb-1.0:
-- - Includes: C:/local/libs/libusbx
-- - Libraries: C:/local/libs/libusbx/libusb-1.0.lib
You have called ADD_LIBRARY for library cryptopp without any source files. This typically indicates a problem with your CMakeLists.txt file
-- Configuring done
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
CMake Error: CMake can not determine linker language for target: cryptopp
-- Generating done
-- Build files have been written to: C:/local/projects/synergy-usb/synergy-through-usb-master
What does this mean? how can I solve it / move on?
Links to the install instructions
both goto 404 errors...
http://synergy-project.org/projects/synergy/wiki/Readme/?hl=pm
http://synergy-project.org/projects/synergy/wiki/Compiling/?hl=pm
Based on a quick browse through the project's CMakeLists, it would seem you're supposed to unpack tools/cryptopp562.zip into a directory called tools/cryptopp562 before running CMake. This should definitely have been mentioned in the projects' installation instructions, if any.
More precisely, the file tools/CMakeLists.txt contains this code:
set(cpp_dir cryptopp562)
file(GLOB cpp_src ${cpp_dir}/*.cpp)
# ...
add_library(cryptopp STATIC ${cpp_src})
This means that the variable cpp_src is filled with source files taken from directory cryptopp562 (relative to the CMakeList's directory, i.e. tools), and is then used to define the source files of library cryptopp. However, the project's distribution only contains tools/cryptopp562.zip. Unpacking that into directory tools/cryptopp562 should provide the missing files.