CMake C++ compile command - c++

I'm currently learning CMake, which I understand to be a build file generator, and I wanted to see the exact command that CMake was running to compile and link my C++ files. For example, if I'm working with a single file main.cpp and I were to compile it with gcc, I would run gcc -o main main.cpp. How would I see the C++ command that CMake is running? I tried make VERBOSE=1 but that only reveals CMake-related commands instead of C++ compiler commands.
// main.cpp
#include <iostream>
int main()
{
std::cout << "Hello world" << std::endl;
}
//CMakeLists.txt
cmake_minimum_required(VERSION 3.2)
project(example LANGUAGES CXX)
add_executable(main main.cpp)
This creates a Makefile with the following contents:
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.14
# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /usr/local/Cellar/cmake/3.14.5/bin/cmake
# The command to remove a file.
RM = /usr/local/Cellar/cmake/3.14.5/bin/cmake -E remove -f
# Escaping for special characters.
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /Users/.../Desktop/cpp
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /Users/.../Desktop/cpp/build
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target rebuild_cache
rebuild_cache:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/local/Cellar/cmake/3.14.5/bin/cmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# Special rule for the target edit_cache
edit_cache:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
/usr/local/Cellar/cmake/3.14.5/bin/ccmake -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /Users/.../Desktop/cpp/build/CMakeFiles /Users/.../Desktop/cpp/build/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /Users/.../Desktop/cpp/build/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named main
# Build rule for target.
main: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 main
.PHONY : main
# fast build rule for target.
main/fast:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
.PHONY : main/fast
main.o: main.cpp.o
.PHONY : main.o
# target to build an object file
main.cpp.o:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/main.cpp.o
.PHONY : main.cpp.o
main.i: main.cpp.i
.PHONY : main.i
# target to preprocess a source file
main.cpp.i:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/main.cpp.i
.PHONY : main.cpp.i
main.s: main.cpp.s
.PHONY : main.s
# target to generate assembly for a file
main.cpp.s:
$(MAKE) -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/main.cpp.s
.PHONY : main.cpp.s
# Help Target
help:
#echo "The following are some of the valid targets for this Makefile:"
#echo "... all (the default if no target is provided)"
#echo "... clean"
#echo "... depend"
#echo "... rebuild_cache"
#echo "... edit_cache"
#echo "... main"
#echo "... main.o"
#echo "... main.i"
#echo "... main.s"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
make VERBOSE=1 returns:
/usr/local/Cellar/cmake/3.14.5/bin/cmake -S/Users/.../Desktop/cpp -B/Users/.../Desktop/cpp/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_progress_start /Users/.../Desktop/cpp/build/CMakeFiles /Users/.../Desktop/cpp/build/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/depend
cd /Users/.../Desktop/cpp/build && /usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_depends "Unix Makefiles" /Users/.../Desktop/cpp /Users/.../Desktop/cpp /Users/.../Desktop/cpp/build /Users/.../Desktop/cpp/build /Users/.../Desktop/cpp/build/CMakeFiles/main.dir/DependInfo.cmake --color=
Dependee "/Users/.../Desktop/cpp/build/CMakeFiles/main.dir/DependInfo.cmake" is newer than depender "/Users/.../Desktop/cpp/build/CMakeFiles/main.dir/depend.internal".
Dependee "/Users/.../Desktop/cpp/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/Users/.../Desktop/cpp/build/CMakeFiles/main.dir/depend.internal".
Scanning dependencies of target main
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -o CMakeFiles/main.dir/main.cpp.o -c /Users/.../Desktop/cpp/main.cpp
[100%] Linking CXX executable main
/usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/main.dir/main.cpp.o -o main
[100%] Built target main
/usr/local/Cellar/cmake/3.14.5/bin/cmake -E cmake_progress_start /Users/.../Desktop/cpp/build/CMakeFiles 0

Related

How to link with binary using mingw32 in wine

I have made this minimal project to reproduce my link problem using mingw32 in wine32:
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project("tst" VERSION 1.1.0)
add_executable("tst" "main.cpp")
add_library("resource" "text.o")
set_target_properties("resource" PROPERTIES LINKER_LANGUAGE CXX)
add_custom_command(OUTPUT "text.o"
MAIN_DEPENDENCY "text"
COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/text" "text"
COMMAND ${CMAKE_LINKER} -r -b binary "text" -o "text.o"
)
set_source_files_properties("text" PROPERTIES GENERATED true EXTERNAL_OBJECT true)
target_link_libraries("tst" "resource")
main.cpp
#include <iostream>
#include <string>
extern char _binary_text_start[];
extern char _binary_text_end[];
int main()
{
std::string text{ _binary_text_start, _binary_text_end };
std::cout << text << std::endl;
}
text
Hello, World!
This project builds and runs using mingw-w64 on Linux; however, when I try to build the project in wine32, I got this output:
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -SZ:\var\cache\deploy\cpp -BZ:\var\cache\deploy\cpp\build\windows --check-build-system CMakeFiles\Makefile.cmake 0
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E cmake_progress_start Z:\var\cache\deploy\cpp\build\windows\CMakeFiles Z:\var\cache\deploy\cpp\build\windows\\CMakeFiles\progress.marks
mingw32-make -f CMakeFiles\Makefile2 all
mingw32-make[1]: Entering directory `Z:/var/cache/deploy/cpp/build/windows'
mingw32-make -f CMakeFiles\resource.dir\build.make CMakeFiles/resource.dir/depend
mingw32-make[2]: Entering directory `Z:/var/cache/deploy/cpp/build/windows'
[ 25%] Generating text.o
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E copy Z:/var/cache/deploy/cpp/text text
Z:\mingw32\bin\ld.exe -r -b binary text -o text.o
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E cmake_depends "MinGW Makefiles" Z:\var\cache\deploy\cpp Z:\var\cache\deploy\cpp Z:\var\cache\deploy\cpp\build\windows Z:\var\cache\deploy\cpp\build\windows Z:\var\cache\deploy\cpp\build\windows\CMakeFiles\resource.dir\DependInfo.cmake --color=
mingw32-make[2]: Leaving directory `Z:/var/cache/deploy/cpp/build/windows'
mingw32-make -f CMakeFiles\resource.dir\build.make CMakeFiles/resource.dir/build
mingw32-make[2]: Entering directory `Z:/var/cache/deploy/cpp/build/windows'
[ 50%] Linking CXX static library libresource.a
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -P CMakeFiles\resource.dir\cmake_clean_target.cmake
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E cmake_link_script CMakeFiles\resource.dir\link.txt --verbose=1
Z:\mingw32\bin\ar.exe qc libresource.a text.o
Z:\mingw32\bin\ranlib.exe libresource.a
mingw32-make[2]: Leaving directory `Z:/var/cache/deploy/cpp/build/windows'
[ 50%] Built target resource
mingw32-make -f CMakeFiles\tst.dir\build.make CMakeFiles/tst.dir/depend
mingw32-make[2]: Entering directory `Z:/var/cache/deploy/cpp/build/windows'
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E cmake_depends "MinGW Makefiles" Z:\var\cache\deploy\cpp Z:\var\cache\deploy\cpp Z:\var\cache\deploy\cpp\build\windows Z:\var\cache\deploy\cpp\build\windows Z:\var\cache\deploy\cpp\build\windows\CMakeFiles\tst.dir\DependInfo.cmake --color=
mingw32-make[2]: Leaving directory `Z:/var/cache/deploy/cpp/build/windows'
mingw32-make -f CMakeFiles\tst.dir\build.make CMakeFiles/tst.dir/build
mingw32-make[2]: Entering directory `Z:/var/cache/deploy/cpp/build/windows'
[ 75%] Building CXX object CMakeFiles/tst.dir/main.cpp.obj
Z:\mingw32\bin\g++.exe -O3 -DNDEBUG -o CMakeFiles\tst.dir\main.cpp.obj -c Z:\var\cache\deploy\cpp\main.cpp
[100%] Linking CXX executable tst.exe
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E cmake_link_script CMakeFiles\tst.dir\link.txt --verbose=1
Z:\cmake-3.19.1-win32-x86\bin\cmake.exe -E rm -f CMakeFiles\tst.dir/objects.a
Z:\mingw32\bin\ar.exe cr CMakeFiles\tst.dir/objects.a #CMakeFiles\tst.dir\objects1.rsp
Z:\mingw32\bin\g++.exe -O3 -DNDEBUG -Wl,--whole-archive CMakeFiles\tst.dir/objects.a -Wl,--no-whole-archive -o tst.exe -Wl,--out-implib,libtst.dll.a -Wl,--major-image-version,0,--minor-image-version,0 #CMakeFiles\tst.dir\linklibs.rsp
z:/mingw32/bin/../lib/gcc/i686-w64-mingw32/10.2.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\tst.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x43): undefined reference to `_binary_text_end'
z:/mingw32/bin/../lib/gcc/i686-w64-mingw32/10.2.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\tst.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x48): undefined reference to `_binary_text_start'
z:/mingw32/bin/../lib/gcc/i686-w64-mingw32/10.2.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\tst.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x68): undefined reference to `_binary_text_start'
z:/mingw32/bin/../lib/gcc/i686-w64-mingw32/10.2.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\tst.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x16f): undefined reference to `_binary_text_start'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [tst.exe] Error 1
mingw32-make[2]: Leaving directory `Z:/var/cache/deploy/cpp/build/windows'
mingw32-make[1]: *** [CMakeFiles/tst.dir/all] Error 2
mingw32-make[1]: Leaving directory `Z:/var/cache/deploy/cpp/build/windows'
mingw32-make: *** [all] Error 2
nm libresource.a
text.o:
0000000d D _binary_text_end
0000000d A _binary_text_size
00000000 D _binary_text_start
objdump -f libresource.a
In archive libresource.a:
text.o: file format pe-i386
architecture: i386, flags 0x00000038:
HAS_DEBUG, HAS_SYMS, HAS_LOCALS
start address 0x00000000
cmake: https://github.com/Kitware/CMake/releases/download/v3.19.1/cmake-3.19.1-win32-x86.zip
mingw: https://pilotfiber.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win32/Personal%20Builds/ray_linn/GCC-10.x-with-ada/mingw32-10.2.0-crt-8.0.0-with-ada.7z
mingw-make: https://phoenixnap.dl.sourceforge.net/project/mingw/MinGW/Extension/make/mingw32-make-3.80-3/mingw32-make-3.80.0-3.tar.gz

CMake OS X CLion. How to link a custom dynamic library?

Probably this question was asked several times. But I can't find a solution. I try to link a shared library and add it to RPATH. I tried several solutions:
Here is my Cmake file:
cmake_minimum_required(VERSION 3.7)
project(Cpp)
set(CMAKE_CXX_STANDARD 11)
file(GLOB CPP_UTILS CppUtils/*.cpp CppUtils/*.h)
set(SOURCE_FILES main.cpp ${CPP_UTILS})
add_executable(Cpp ${SOURCE_FILES})
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
target_link_libraries(Cpp /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/libs/libpython3.7m.dylib)
include_directories(Include)
And it outputs the following error in runtime:
dyld: Library not loaded: /usr/local/lib/libpython3.7m.dylib
Referenced from: /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/Cpp
Reason: image not found
Temporary I just want link a custom library, to understand how it works. Then I want to copy the libs folder while build execution.
This is what it outputs when I try to run the command manually make VERBOSE=1
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp -B/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Cpp.dir/build.make CMakeFiles/Cpp.dir/depend
cd /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles/Cpp.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Cpp.dir/build.make CMakeFiles/Cpp.dir/build
make[2]: Nothing to be done for `CMakeFiles/Cpp.dir/build'.
[100%] Built target Cpp
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles 0
yo:cmake-build-debug stikhonenko$ make clean
yo:cmake-build-debug stikhonenko$ make VERBOSE=1
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -H/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp -B/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Cpp.dir/build.make CMakeFiles/Cpp.dir/depend
cd /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug && /Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_depends "Unix Makefiles" /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles/Cpp.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Cpp.dir/build.make CMakeFiles/Cpp.dir/build
[ 25%] Building CXX object CMakeFiles/Cpp.dir/main.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/Include -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -std=gnu++11 -o CMakeFiles/Cpp.dir/main.cpp.o -c /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/main.cpp
[ 50%] Building CXX object CMakeFiles/Cpp.dir/CppUtils/System.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/Include -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -std=gnu++11 -o CMakeFiles/Cpp.dir/CppUtils/System.cpp.o -c /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/CppUtils/System.cpp
[ 75%] Building CXX object CMakeFiles/Cpp.dir/CppUtils/TimeUtils.cpp.o
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -I/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/Include -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -std=gnu++11 -o CMakeFiles/Cpp.dir/CppUtils/TimeUtils.cpp.o -c /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/CppUtils/TimeUtils.cpp
[100%] Linking CXX executable Cpp
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_link_script CMakeFiles/Cpp.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Cpp.dir/main.cpp.o CMakeFiles/Cpp.dir/CppUtils/System.cpp.o CMakeFiles/Cpp.dir/CppUtils/TimeUtils.cpp.o -o Cpp ../libs/libpython3.7m.dylib
[100%] Built target Cpp
/Applications/CLion.app/Contents/bin/cmake/bin/cmake -E cmake_progress_start /Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/cmake-build-debug/CMakeFiles 0
You first need to tell cmake where to find the library (find_library), and only then you can use the result from find_library in target_link_libraries
find_library takes a PATHS argument which you can use to tell cmake where to look
find_library(
PYTHON_3
libpython3.7m
PATHS
/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/libs)
Now you will have a variable ${PYTHON_3} which contains the path to the library. You use that variable in target_link_libraries
target_link_libraries(
Cpp
${PYTHON_3})
Here is the complete CMakeLists.txt:
cmake_minimum_required(VERSION 3.7)
project(Cpp)
set(CMAKE_CXX_STANDARD 11)
file(GLOB CPP_UTILS CppUtils/*.cpp CppUtils/*.h)
set(SOURCE_FILES main.cpp ${CPP_UTILS})
add_executable(Cpp ${SOURCE_FILES})
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
find_library(
PYTHON_3
libpython3.7m
PATHS
/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/libs)
target_link_libraries(Cpp ${PYTHON_3})
target_include_directories(Cpp Include)
Lets try linking the library's directory!
Assuming the name of your library is libpython3
cmake_minimum_required(VERSION 3.7)
project(Cpp)
set(CMAKE_CXX_STANDARD 11)
file(GLOB CPP_UTILS CppUtils/*.cpp CppUtils/*.h)
set(SOURCE_FILES main.cpp ${CPP_UTILS})
add_executable(Cpp ${SOURCE_FILES})
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
LINK_DIRECTORIES(/Users/mac/Projects/ECMCalmnessScroreAlgo/Cpp/libs)
target_link_libraries(Cpp libpython3.7m)
include_directories(Include)
Does this work? :-)

Why does cmake not add a library link command in the makefile?

I Ubuntu, I am learning about cmake and make, and just trying a simple example. I have two directories: src and build. In src, I have two files: main.cpp, with just a simple hello world example, and CMakeLists.txt, with the following text:
add_executable(test main.cpp)
target_link_libraries(test blas)
Here, blas is just some arbitrary library that I've found at /usr/lib/libblas.so. My main.cpp file does not actually use it, but I am just testing things out...
Now, if I enter the build directory, and run cmake ../src, then a makefile is generated in build. The contents of that makefile is pasted below.
What I am puzzled about, is why there is no mention of the blas library at all? Shouldn't cmake be adding a line such as -lblas?
Thanks :)
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake
# The command to remove a file.
RM = /usr/bin/cmake -E remove -f
# Escaping for special characters.
EQUALS = =
# The program to use to edit the cache.
CMAKE_EDIT_COMMAND = /usr/bin/cmake-gui
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/ejohns/Projects/Test2/src
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/ejohns/Projects/Test2/build
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target edit_cache
edit_cache:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
/usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# Special rule for the target rebuild_cache
rebuild_cache:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/ejohns/Projects/Test2/build/CMakeFiles /home/ejohns/Projects/Test2/build/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/ejohns/Projects/Test2/build/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named test
# Build rule for target.
test: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 test
.PHONY : test
# fast build rule for target.
test/fast:
$(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
.PHONY : test/fast
main.o: main.cpp.o
.PHONY : main.o
# target to build an object file
main.cpp.o:
$(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/main.cpp.o
.PHONY : main.cpp.o
main.i: main.cpp.i
.PHONY : main.i
# target to preprocess a source file
main.cpp.i:
$(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/main.cpp.i
.PHONY : main.cpp.i
main.s: main.cpp.s
.PHONY : main.s
# target to generate assembly for a file
main.cpp.s:
$(MAKE) -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/main.cpp.s
.PHONY : main.cpp.s
# Help Target
help:
#echo "The following are some of the valid targets for this Makefile:"
#echo "... all (the default if no target is provided)"
#echo "... clean"
#echo "... depend"
#echo "... edit_cache"
#echo "... rebuild_cache"
#echo "... test"
#echo "... main.o"
#echo "... main.i"
#echo "... main.s"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
CMake uses a 3-level hierarchy of makefiles. The line you're looking for will be either in CMakeFiles/Makefile2 or in build.make somewhere under CMakeFiles (and probably a test directoty as well).

How to compile jsoncpp with -fPIC through cmake

I want to compile jsoncpp with -fPIC through cmake.
In the website for jsoncpp, it is explaining the way normally to compile like below.
mkdir -p build/debug
cd build/debug
cmake -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../..
make
How can i compile with -fPIC flag in this step?
---------------------------------------------------------Edit-----------------------------------------------------------
I assume i have to put some line in makefile for -fPIC.
So i add the content of makefile by cmake.
how can i change this makefile for -fPIC flag?
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 2.8
# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
# The shell in which to execute make rules.
SHELL = /bin/sh
# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake
# The command to remove a file.
RM = /usr/bin/cmake -E remove -f
# The program to use to edit the cache.
CMAKE_EDIT_COMMAND = /usr/bin/cmake-gui
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/kim/jsoncpp-pre-C-11
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/kim/jsoncpp-pre-C-11/build/debug
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target edit_cache
edit_cache:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
/usr/bin/cmake-gui -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# Special rule for the target install
install: preinstall
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
/usr/bin/cmake -P cmake_install.cmake
.PHONY : install
# Special rule for the target install
install/fast: preinstall/fast
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..."
/usr/bin/cmake -P cmake_install.cmake
.PHONY : install/fast
# Special rule for the target install/local
install/local: preinstall
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing only the local directory..."
/usr/bin/cmake -DCMAKE_INSTALL_LOCAL_ONLY=1 -P cmake_install.cmake
.PHONY : install/local
# Special rule for the target install/local
install/local/fast: install/local
.PHONY : install/local/fast
# Special rule for the target install/strip
install/strip: preinstall
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing the project stripped..."
/usr/bin/cmake -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake
.PHONY : install/strip
# Special rule for the target install/strip
install/strip/fast: install/strip
.PHONY : install/strip/fast
# Special rule for the target list_install_components
list_install_components:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Available install components are: \"Unspecified\""
.PHONY : list_install_components
# Special rule for the target list_install_components
list_install_components/fast: list_install_components
.PHONY : list_install_components/fast
# Special rule for the target rebuild_cache
rebuild_cache:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# Special rule for the target test
test:
#$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running tests..."
/usr/bin/ctest --force-new-ctest-process $(ARGS)
.PHONY : test
# Special rule for the target test
test/fast: test
.PHONY : test/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start /home/kim/jsoncpp-pre-C-11/build/debug/CMakeFiles /home/kim/jsoncpp-pre-C-11/build/debug/CMakeFiles/progress.marks
$(MAKE) -f CMakeFiles/Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start /home/kim/jsoncpp-pre-C-11/build/debug/CMakeFiles 0
.PHONY : all
# The main clean target
clean:
$(MAKE) -f CMakeFiles/Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles/Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named jsoncpp_lib
# Build rule for target.
jsoncpp_lib: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 jsoncpp_lib
.PHONY : jsoncpp_lib
# fast build rule for target.
jsoncpp_lib/fast:
$(MAKE) -f src/lib_json/CMakeFiles/jsoncpp_lib.dir/build.make src/lib_json/CMakeFiles/jsoncpp_lib.dir/build
.PHONY : jsoncpp_lib/fast
#=============================================================================
# Target rules for targets named jsoncpp_check
# Build rule for target.
jsoncpp_check: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 jsoncpp_check
.PHONY : jsoncpp_check
# fast build rule for target.
jsoncpp_check/fast:
$(MAKE) -f src/jsontestrunner/CMakeFiles/jsoncpp_check.dir/build.make src/jsontestrunner/CMakeFiles/jsoncpp_check.dir/build
.PHONY : jsoncpp_check/fast
#=============================================================================
# Target rules for targets named jsoncpp_readerwriter_tests
# Build rule for target.
jsoncpp_readerwriter_tests: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 jsoncpp_readerwriter_tests
.PHONY : jsoncpp_readerwriter_tests
# fast build rule for target.
jsoncpp_readerwriter_tests/fast:
$(MAKE) -f src/jsontestrunner/CMakeFiles/jsoncpp_readerwriter_tests.dir/build.make src/jsontestrunner/CMakeFiles/jsoncpp_readerwriter_tests.dir/build
.PHONY : jsoncpp_readerwriter_tests/fast
#=============================================================================
# Target rules for targets named jsontestrunner_exe
# Build rule for target.
jsontestrunner_exe: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 jsontestrunner_exe
.PHONY : jsontestrunner_exe
# fast build rule for target.
jsontestrunner_exe/fast:
$(MAKE) -f src/jsontestrunner/CMakeFiles/jsontestrunner_exe.dir/build.make src/jsontestrunner/CMakeFiles/jsontestrunner_exe.dir/build
.PHONY : jsontestrunner_exe/fast
#=============================================================================
# Target rules for targets named jsoncpp_test
# Build rule for target.
jsoncpp_test: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 jsoncpp_test
.PHONY : jsoncpp_test
# fast build rule for target.
jsoncpp_test/fast:
$(MAKE) -f src/test_lib_json/CMakeFiles/jsoncpp_test.dir/build.make src/test_lib_json/CMakeFiles/jsoncpp_test.dir/build
.PHONY : jsoncpp_test/fast
# Help Target
help:
#echo "The following are some of the valid targets for this Makefile:"
#echo "... all (the default if no target is provided)"
#echo "... clean"
#echo "... depend"
#echo "... edit_cache"
#echo "... install"
#echo "... install/local"
#echo "... install/strip"
#echo "... list_install_components"
#echo "... rebuild_cache"
#echo "... test"
#echo "... jsoncpp_lib"
#echo "... jsoncpp_check"
#echo "... jsoncpp_readerwriter_tests"
#echo "... jsontestrunner_exe"
#echo "... jsoncpp_test"
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system
You have to change the CMAKE_CXX_FLAGS and the CMAKE_C_FLAGS variable. You can do that via the ccmake or cmake-gui (you'll have to toggle advanced) or you can provide it as you were doing:
cmake -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=debug -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=OFF -G "Unix Makefiles" ../..
Don't bother changing the Makefile, as it would be overwritten the next time you ran cmake.
In CMake 2.8.9 you can set the POSITION_INDEPENDENT_CODE property
on a target to make all following targets position independent. Or you can set the global variable within the CMakeLists.txt
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
or
cmake -DCMAKE_POSITION_INDEPENDENT_CODE=ON ..

Errors compiling MinGW for CMake

after making a breakthrough with my earlier MiniGW problem i have come across another. This is very diffrent from my understanding. from what i see everything compiles ok and i dont have a good enough understanding of cmake to know what the problem is.
here is the Error log
||=== Build: Icarus in Icarus (compiler: GNU GCC Compiler) ===|
CMakeFiles\Makefile2|60|recipe for target 'CMakeFiles/Icarus.dir/all' failed|
CMakeFiles\Makefile2|72|recipe for target 'CMakeFiles/Icarus.dir/rule' failed|
D:\Projects\SVN\Icarus\build\Makefile|110|recipe for target 'Icarus' failed|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Here is the line in the makefile it points to
# Build rule for target.
Icarus: cmake_check_build_system
$(MAKE) -f CMakeFiles/Makefile2 Icarus
.PHONY : Icarus
And here is the first section it points to in makefile2
CMakeFiles/Icarus.dir/all:
$(MAKE) -f CMakeFiles/Icarus.dir/build.make CMakeFiles/Icarus.dir/depend
$(MAKE) -f CMakeFiles/Icarus.dir/build.make CMakeFiles/Icarus.dir/build
$(CMAKE_COMMAND) -E cmake_progress_report D:/Projects/SVN/Icarus/build/CMakeFiles
#echo "Built target Icarus"
.PHONY : CMakeFiles/Icarus.dir/all
And finally here is the final section
# Build rule for subdir invocation for target.
CMakeFiles/Icarus.dir/rule: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start D:/Projects/SVN/Icarus/build/CMakeFiles 31
$(MAKE) -f CMakeFiles/Makefile2 CMakeFiles/Icarus.dir/all
$(CMAKE_COMMAND) -E cmake_progress_start D:/Projects/SVN/Icarus/build/CMakeFiles 0
.PHONY : CMakeFiles/Icarus.dir/rule
hopefully this is enough information to fix the error.
Edit information from about the error
Running command: c:/MinGW/bin/make.exe -f "D:/Projects/SVN/Icarus/build/Makefile" VERBOSE=1 Icarus
"c:/Program Files (x86)/CMake/bin/cmake.exe" -HD:/Projects/SVN/Icarus -BD:/Projects/SVN/Icarus/build --check-build-system CMakeFiles/Makefile.cmake 0
c:/MinGW/bin/make.exe -f CMakeFiles/Makefile2 Icarus
make.exe[1]: Entering directory 'D:/Projects/SVN/Icarus/build'
"c:/Program Files (x86)/CMake/bin/cmake.exe" -HD:/Projects/SVN/Icarus -BD:/Projects/SVN/Icarus/build --check-build-system CMakeFiles/Makefile.cmake 0
"c:/Program Files (x86)/CMake/bin/cmake.exe" -E cmake_progress_start D:/Projects/SVN/Icarus/build/CMakeFiles 31
c:/MinGW/bin/make.exe -f CMakeFiles/Makefile2 CMakeFiles/Icarus.dir/all
make.exe[2]: Entering directory 'D:/Projects/SVN/Icarus/build'
c:/MinGW/bin/make.exe -f CMakeFiles/Icarus.dir/build.make CMakeFiles/Icarus.dir/depend
make.exe[3]: Entering directory 'D:/Projects/SVN/Icarus/build'
"C:\Program Files (x86)\CMake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" D:\Projects\SVN\Icarus D:\Projects\SVN\Icarus D:\Projects\SVN\Icarus\build D:\Projects\SVN\Icarus\build D:\Projects\SVN\Icarus\build\CMakeFiles\Icarus.dir\DependInfo.cmake --color=
make.exe[3]: Leaving directory 'D:/Projects/SVN/Icarus/build'