Per the set_directory_properties docs, properties set on the directory are supposed to propagate to subdirectories:
Set a property for the current directory and subdirectories.
Per the supported properties documentation, COMPILE_DEFINITIONS is a property supported by directories.
Given this, why doesn't COMPILE_DEFINITIONS for a directory propagate to subdirectories in the following example?
Sample Project File Structure
- CMakeLists.txt
- sub
-CMakeLists.txt
-main.cpp
File Contents
Root CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.11)
project(cmake_sandbox)
add_subdirectory(sub)
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS SHOW_MESSAGE=1)
Sub CMakeLists.txt:
add_executable(hello main.cpp)
main.cpp:
#include <iostream>
using namespace std;
int main()
{
#define A_LOCAL_MESSAGE
#ifdef A_LOCAL_MESSAGE
#pragma message("A local message!")
#else
#pragma message("No local message!")
#endif
#ifdef SHOW_MESSAGE
#pragma message("A message!")
#else
#pragma message("No message!")
#endif
cout << "Hello, World!\n";
return 0;
}
Testing
$ cmake --version
cmake version 3.10.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ rm -rf build && mkdir build && cd build && cmake .. && make -j && sub/hello
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/caleb/src/cmake-sandbox/build
Scanning dependencies of target hello
[ 50%] Building CXX object sub/CMakeFiles/hello.dir/main.cpp.o
/home/caleb/src/cmake-sandbox/sub/main.cpp: In function ‘int main()’:
/home/caleb/src/cmake-sandbox/sub/main.cpp:8:36: note: #pragma message: A local message!
#pragma message("A local message!")
^
/home/caleb/src/cmake-sandbox/sub/main.cpp:16:31: note: #pragma message: No message!
#pragma message("No message!")
^
[100%] Linking CXX executable hello
[100%] Built target hello
Hello, World!
If the COMPILE_DEFINITION set at the root level had propagated as expected, the second pragma output would have changed to "A message!" case. Why isn't this happening?
add_subdirectory causes CMake to enter the subdirectory and process the CMakeLists.txt file in there before processing the directives that follow add_subdirectory.
If you want those settings picked up you need to set them before you recurse into a subdirectory.
set_directory_properties(PROPERTIES COMPILE_DEFINITIONS SHOW_MESSAGE=1)
add_subdirectory(sub)
Related
I installed vcpkg on macOS and I'm trying to build a simple library that depends on fmt, which I installed with vcpkg.
mylib.h
float add(float a, float b);
mylib.cpp
#include "mylib.h"
#include <iostream>
#include <fmt/core.h>
float add(float a, float b)
{
fmt::print("Hello MYLIB, world!\n");
return (a + b);
}
CMakeLists.txt contents:
cmake_minimum_required(VERSION 3.19.1)
project(MYLIB)
find_package(fmt REQUIRED)
add_library(mylib mylib.cpp)
Then
user#users-MacBook-Pro build % cmake -B . -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake -S ..
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user/mylib/build
user#users-MacBook-Pro build % make
Scanning dependencies of target mylib
[ 50%] Building CXX object CMakeFiles/mylib.dir/mylib.cpp.o
/Users/user/mylib/mylib.cpp:5:10: fatal error: 'fmt/core.h' file not found
#include <fmt/core.h>
^~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/mylib.dir/mylib.cpp.o] Error 1
make[1]: *** [CMakeFiles/mylib.dir/all] Error 2
make: *** [all] Error 2
What am I missing?
I tried the same on Windows and it works fine. On Windows though we run vcpkg integrate install which does not exist on macOS. Is this related to the problem?
Looks like it's necessary to
include_directories(~/vcpkg/installed/x64-osx/include)
You are using the variable CMAKE_TOOLCHAIN_FILE incorrectly. set(CMAKE_TOOLCHAIN_FILE ... in CMakeLists.txt has no effect. The variable should be set on the command line, see the manuals CMAKE_TOOLCHAIN_FILE, Using vcpkg with CMake
cmake .. -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake
The file CMakeLists.txt is also wrong, find_package(fmt REQUIRED) is missing, that should download and install fmt by invoking vcpkg install fmt under the hood.
After all you should link your project with the lib
target_link_libraries(MYLIB PRIVATE fmt::fmt)
When using this CMakeLists.txt
cmake_minimum_required(VERSION 3.17)
project(project_c)
set(CMAKE_CXX_STANDARD 11)
set(project_name project_c)
find_package(BISON)
find_package(FLEX)
BISON_TARGET(parser parser.y ${CMAKE_SOURCE_DIR}/parser.cpp)
FLEX_TARGET(lexer lexer.l ${CMAKE_SOURCE_DIR}/lexer.cpp)
ADD_FLEX_BISON_DEPENDENCY(lexer parser)
add_executable(${project_name} ${BISON_parser_OUTPUTS} ${FLEX_lexer_OUTPUTS})
target_include_directories(${project_name} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}")
target_link_libraries(${project_name} ${FLEX_LIBRARIES})
CMake complains about
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
FL_LIBRARY (ADVANCED)
linked by target "project_c" in directory D:/asant/workspace/CLionProjects/project_c
I've tried to copy the winflex folder inside the project folder but that won't help anyway. This proposed solution isn't working.
This is the complete CMake log
"C:\Program Files\JetBrains\CLion 2018.3.4\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" D:\asant\workspace\CLionProjects\project_c
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Check for working C compiler: C:/Qt/Tools/mingw810_64/bin/gcc.exe
-- Check for working C compiler: C:/Qt/Tools/mingw810_64/bin/gcc.exe - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Qt/Tools/mingw810_64/bin/g++.exe
-- Check for working CXX compiler: C:/Qt/Tools/mingw810_64/bin/g++.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found BISON: C:/Program Files (x86)/win_flex_bison-2.5.23/win_bison.exe (found version "3.7.1")
-- Found FLEX: C:/Program Files (x86)/win_flex_bison-2.5.23/win_flex.exe (found version "2.6.4")
-- Configuring done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
FL_LIBRARY (ADVANCED)
linked by target "project_c" in directory D:/asant/workspace/CLionProjects/project_c
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
[Failed to reload]
I have cloned the RealSense official source code from their Github page. There was a problem when I was compiling the example align-advanced. The following is my terminal situation:
$ cd ~/librealsense/examples/align-advanced
$ mkdir build
$ cd build
~/librealsense/examples/align-advanced/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/automation/librealsense/examples/align-advanced/build
~/librealsense/examples/align-advanced/build$ make
~/librealsense/examples/align-advanced/build$
As you can see, nothing happens after typing make, and no executable files appear in the build folder.
The contents of offical CMakeList.txt are as follows:
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2019 Intel Corporation. All Rights Reserved.
# minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)
project(RealsenseExamplesAlignAdvanced)
if(BUILD_GRAPHICAL_EXAMPLES)
add_executable(rs-align-advanced rs-align-advanced.cpp ../../third-party/imgui/imgui.cpp ../../third-party/imgui/imgui_draw.cpp ../../third-party/imgui/imgui_impl_glfw.cpp)
set_property(TARGET rs-align-advanced PROPERTY CXX_STANDARD 11)
target_link_libraries(rs-align-advanced ${DEPENDENCIES})
include_directories(rs-align-advanced ../../common ../../third-party/imgui)
set_target_properties (rs-align-advanced PROPERTIES FOLDER Examples)
install(TARGETS rs-align-advanced RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
What is the problem?
CMake was intended to be run from the top-level directory of the entire project. This is a general CMake rule-of-thumb; it typically applies to any CMake-based project, not only RealSense.
If you want to enable the compilation of the RealSense examples, you can control it via CMake with the BUILD_EXAMPLES variable, as seen the Build Configuration documentation. This only builds a small subset of the examples, however. To build the align-advanced examples, you also need to set BUILD_GRAPHICAL_EXAMPLES:
cd ~/librealsense
mkdir build; cd build
cmake -DBUILD_EXAMPLES=true -DBUILD_GRAPHICAL_EXAMPLES=true ..
make
I am trying to compile my source code using riscv64-unknown-elf-g++ compiler into a binary file but I am getting this warning and unable to generate exec file:
warning for library: libofdm.a the table of contents is empty (no object file members in the library define global symbols)
referring to this issue before:
Can't make my c++ file into a library with compiler
as suggested in the above answer the same toolchain is used to compile the object file to create the library. Even though I am getting the error. Below I am attaching the whole command line used to compile.
Last login: Thu Jul 9 12:50:56 on ttys000
The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
(base) k:~ cd /Users/yuli/Documents/version3/cpp/
(base) k:~ ls
CMakeLists.txt src test
(base) k:~ mkdir build_riscv
(base) k:~ cd build_riscv
(base) k:~ cmake ..
-- The C compiler identification is AppleClang 11.0.3.11030032
-- The CXX compiler identification is AppleClang 11.0.3.11030032
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Compiling RISCV
CMAKE_CXX_COMPILER: /usr/local/opt/riscv-gnu-toolchain/bin/riscv64-unknown-elf-g++
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/yuli/Documents/version3/cpp/build_riscv
(base) k:~ make
Scanning dependencies of target ofdm
[ 33%] Building CXX object CMakeFiles/ofdm.dir/src/transmitter.cpp.o
[ 66%] Building CXX object CMakeFiles/ofdm.dir/src/configuration.cpp.o
[100%] Linking CXX static library libofdm.a
warning: /Library/Developer/CommandLineTools/usr/bin/ranlib: warning for library: libofdm.a the table of contents is empty (no object file members in the library define global symbols)
[100%] Built target ofdm
(base) k:~
and FYI I am using mac machine. I am Specifying this because I saw in a post with "clang warnings macOS #3331".
clang warnings macOS #3331
CMakeLists.txt as follows:
cmake_minimum_required(VERSION 3.13)
project(ofdmchain)
set(CMAKE_CXX_COMPILER "/usr/local/opt/riscv-gnu-toolchain/bin/riscv64-unknown-elf-g++")
add_definitions(-DCOMPILES_ON_PC -Wall -Wextra)
configure_file(${CMAKE_SOURCE_DIR}/src/config.hpp.in ${CMAKE_BINARY_DIR}/config.hpp)
if("${CMAKE_CXX_COMPILER}" MATCHES ".*riscv64-unknown-elf-g\\+\\+")
message(STATUS "Compiling RISCV")
SET(RISCV 1)
else()
message(STATUS "Compiling X86")
SET(RISCV 0)
endif()
message("CMAKE_CXX_COMPILER: ${CMAKE_CXX_COMPILER}")
add_library(ofdm
src/transmitter.cpp
src/configuration.cpp
src/ofdm.hpp
src/datatypes.hpp
)
target_include_directories(ofdm PRIVATE ${CMAKE_SOURCE_DIR}/../reference_matlab)
target_include_directories(ofdm PRIVATE ${CMAKE_SOURCE_DIR}/src/)
if (NOT ${RISCV})
add_executable(unit_tests
test/catch_main.cpp
test/test_sanity.cpp
test/test_utilities.cpp
test/test_transmitter.cpp
)
target_include_directories(unit_tests PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(unit_tests PUBLIC ${CMAKE_SOURCE_DIR}/src)
target_link_libraries(unit_tests ofdm)
endif()
I'm trying to compile a cpp code which uses libavcodec:
#include <libavcodec/avcodec.h>
I tried all variations of
find_package()
with names like ffmpeg for which I get
CMake Warning at CMakeLists.txt:17 (find_package):
By not providing "Findffmpeg.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "ffmpeg", but
CMake did not find one.
Could not find a package configuration file provided by "ffmpeg" with any
of the following names:
ffmpegConfig.cmake
ffmpeg-config.cmake
Add the installation prefix of "ffmpeg" to CMAKE_PREFIX_PATH or set
"ffmpeg_DIR" to a directory containing one of the above files. If "ffmpeg"
provides a separate development package or SDK, be sure it has been
installed.
By the way, I did sudo apt install -y ffmpeg before all that.
I'm compiling with
add_executable(my_executable ${sources})
Minimal working example
CMakeLists.txt
cmake_minimum_required(VERSION 3.1.0)
project(hello)
find_library(AVCODEC_LIBRARY avcodec)
add_executable(hello main.cpp)
target_link_libraries( hello PRIVATE ${AVCODEC_LIBRARY})
main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World!";
return 0;
}
Output:
lz#vm:~/mcve$ cmake .
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
AVCODEC_LIBRARY
linked by target "hello" in directory /home/lz/mcve
-- Configuring incomplete, errors occurred!
See also "/home/lz/mcve/CMakeFiles/CMakeOutput.log".
This is what was missing (even though libavcodec appeared in some folders of my system)
sudo apt install -y libavcodec-dev libavformat-dev libavdevice-dev libavfilter-dev