I have such files
.
├── CMakeLists.txt
└── src
├── CMakeLists.txt
└── main.c
And this is the content about this files
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
PROJECT (HELLO)
ADD_SUBDIRECTORY(src bin)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/binarydir)
$ cat src/CMakeLists.txt
ADD_EXECUTABLE(hello main.c)
$ cat src/main.c
#include <stdio.h>
int main()
{
printf("Hello World from t1 main().\n");
return 0;
}
Then I build it with following command
$ mkdir build
$ cd build
$ cmake ..
$ make
This is the result directory structure
Then the binary hello will produced in directory build/bin as the picture, but it should be in build/binarydir since I have set the value for EXECUTABLE_OUTPUT_PATH, isn't it? What I have missed?
You are creating executable target before setting EXECUTABLE_OUTPUT_PATH. Move SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/binarydir) line before ADD_SUBDIRECTORY(src bin).
Related
I have tbb downloaded and placed in my repository directory:
> tree deps/tbb/ -d
deps/tbb/
├── bin
├── cmake
│ └── templates
├── include
│ ├── serial
│ │ └── tbb
│ └── tbb
│ ├── compat
│ ├── internal
│ └── machine
└── lib
├── ia32
│ └── gcc4.8
└── intel64
└── gcc4.8
In my CMakeLists.txt I have tried this:
include_directories("deps/tbb/include")
find_library(TBB_LIB
NAMES
tbbbind_debug
tbbbind
tbb_debug
tbbmalloc_debug
tbbmalloc_proxy_debug
tbbmalloc_proxy
tbbmalloc
tbb_preview_debug
tbb_preview
tbb
HINTS "${CMAKE_PREFIX_PATH}/deps/tbb/lib/intel64/gcc4.8"
)
add_executable(${PROJECT_NAME}
src/main.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC ${TBB_LIB})
But building with cmake, linker throws this error:
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lTBB_LIB-NOTFOUND
collect2: error: ld returned 1 exit status
I couldn't figure out what is missing. Thanks.
Update
This commit resolves the previous error:
- HINTS "${CMAKE_PREFIX_PATH}/deps/tbb/lib/intel64/gcc4.8"
+ HINTS "deps/tbb/lib/intel64/gcc4.8"
But, new errors are thrown:
undefined reference to `tbb::interface7::internal::task_arena_base::internal_current_slot()'
Update
Other than find_library, what CMake tools are available to link to TBB shared libraries?
I have tried some CMake tools, but I cannot figure out how to link to TBB *.so files correctly!
TBB has native CMake support. On my system with the Intel oneAPI installed, the config package is installed here:
/opt/intel/oneapi/tbb/latest/lib/cmake/tbb/TBBConfig.cmake
Therefore, I just need to add /opt/intel/oneapi/tbb/latest to my CMAKE_PREFIX_PATH. In my CMakeLists.txt, I wrote this:
cmake_minimum_required(VERSION 3.21)
project(test-tbb)
find_package(TBB REQUIRED)
add_library(main main.cpp)
target_link_libraries(main PRIVATE TBB::tbb)
target_compile_features(main PRIVATE cxx_std_11)
TBB provides the IMPORTED target TBB::tbb, which is what you should link to.
main.cpp is just the source from here: https://stackoverflow.com/a/36782794/2137996
Build like so:
$ cmake -G Ninja -S . -B build -DCMAKE_PREFIX_PATH=/opt/intel/oneapi/tbb/latest
$ cmake --build build --verbose
[1/2] /usr/bin/c++ -isystem /opt/intel/oneapi/tbb/2021.3.0/include -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/alex/test2/main.cpp
[2/2] : && /usr/bin/cmake -E rm -f libmain.a && /usr/bin/ar qc libmain.a CMakeFiles/main.dir/main.cpp.o && /usr/bin/ranlib libmain.a && :
Inspired by #AlexReinking answer, here is the final implementation:
project(my-cpp-service VERSION 0.1.0)
# Equivalent to command-line option of `-DCMAKE_PREFIX_PATH=...`
list(APPEND CMAKE_MODULE_PATH "deps/tbb/cmake/")
find_package(TBB REQUIRED)
add_executable(${PROJECT_NAME}
src/main.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC
TBB::tbb
)
This post helped me solved the problem:
https://stackoverflow.com/a/41909627/3405291
The errors got resolved by:
include_directories("deps/tbb/include")
# https://stackoverflow.com/a/41909627/3405291
find_library(LIB_TBB NAMES tbb HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBbind NAMES tbbbind HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBmalloc_proxy NAMES tbbmalloc_proxy HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBmalloc NAMES tbbmalloc HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBB_preview NAMES tbb_preview HINTS "deps/tbb/lib/intel64/gcc4.8")
add_executable(${PROJECT_NAME}
src/main.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC
${LIB_TBB}
${LIB_TBBbind}
${LIB_TBBmalloc_proxy}
${LIB_TBBmalloc}
${LIB_TBB_preview}
)
I've noticed that when I try to build a c++ project with some flags, e.g., "-Wall -Wmissing-include-dirs", bazel gives a missing include directory.
The project structure is the following
root
├── src
│ ├── CMakeLists.txt
│ ├── BUILD.bazel
│ └── main.cpp
├── CMakeLists.txt
├── README.md
└── WORKSPACE
src/main.cpp
#include<iostream>
int main()
{
std::cout << "Compiled\n";
return 0;
}
src/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(main)
set(CMAKE_CXX_FLAGS "-Wall -Wmissing-include-dirs -Ipath/that/doesnt/exist")
set(SOURCES main.cpp)
add_executable( main ${SOURCES})
src/BUILD.bazel
cc_binary(
name = "main",
srcs = ["main.cpp"],
copts = ["-Ipath/that/doesnt/exist -Wall -Wmissing-include-dirs"],
)
When compiling main.cpp with cmake (mkdir build && cd build && cmake .. && make), I see an expected warning for path/that/doesnt/exist (since it doesn't exist), e.g.,
cc1plus: warning: path/that/doesnt/exist: No such file or directory [-Wmissing-include-dirs]
but when I compile with bazel (bazel build //src:main) I get an additional missing include
cc1plus: warning: path/that/doesnt/exist: No such file or directory [-Wmissing-include-dirs]
cc1plus: warning: bazel-out/k8-fastbuild/bin/external/bazel_tools: No such file or directory [-Wmissing-include-dirs]
Question 1: Where does bazel-out/k8-fastbuild/bin/external/bazel_tools come from and why is it missing?
Question 2: How do I write my bazel build so that this warning doesn't appear?
If I were to remove the inclusion of path/that/doesnt/exist from the cmake and bazel builds, and compile with flags -Wall -Werror -Wmissing-include-dirs, then the cmake build succeeds, but the bazel one fails.
Minimum working example found here: https://github.com/btk-learning/learn-to-build-cmake-bazel
I have a simple c++ project which includes eigen. I'm able to compile the project on my own machine but having trouble to compile it to webassembly with emscripten.
Project structure:
.
├── CMakeLists.txt
├── include
│ └── HelloWasm
│ └── my_lib.h
└── src
├── main.cpp
└── my_lib.cpp
File contents:
CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )
project( HelloWasm )
# flags
# include files
include_directories( ./include .include/HelloWasm ./src )
# target
add_executable( HelloWasm ./src/main.cpp ./src/my_lib.cpp )
# 3rd party libs
find_package(Eigen3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})
include/HelloWasm/my_lib.h
#include <iostream>
#include <Eigen/Dense>
using namespace std;
using Eigen::MatrixXd;
class MyLib
{
private:
protected:
public:
MyLib()
{
}
~MyLib()
{
}
void eigen_test();
};
src/main.cpp
#include <iostream>
#include "HelloWasm/my_lib.h"
using namespace std;
int main()
{
MyLib my_lib;
my_lib.eigen_test();
}
src/my_lib.cpp
#include "HelloWasm/my_lib.h"
void MyLib::eigen_test()
{
MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = m(1, 0) + m(0, 1);
cout << '\n'
<< m << endl;
}
Compiling the project successfully locally:
mkdir build && cd build
cmake ..
make
➜ ./HelloWasm
3 -1
2.5 1.5
Errors when trying to compile to webassemply
(I tried following the steps provided in the emscripten docs)
mkdir build && cd build
cmake ..
emcmake cmake ..
output:
configure: cmake .. -DCMAKE_TOOLCHAIN_FILE=/Users/me/programming/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/Users/me/programming/emsdk/node/12.9.1_64bit/bin/node"
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_TOOLCHAIN_FILE
-- Build files have been written to: /Users/me/programming/sandbox/cpp_sandbox/so_question_project/build
Now running make:
➜ emmake make
make: make
Scanning dependencies of target HelloWasm
[ 33%] Building CXX object CMakeFiles/HelloWasm.dir/src/main.cpp.o
[ 66%] Building CXX object CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o
[100%] Linking CXX executable HelloWasm
[100%] Built target HelloWasm
Obviously that did not create a .wasm file...
Doing the following:
em++ CMakeFiles/HelloWasm.dir/src/main.cpp.o CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o -o helloWasm.js
output:
em++: warning: CMakeFiles/HelloWasm.dir/src/main.cpp.o is not a valid input file [-Winvalid-input]
em++: warning: CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o is not a valid input file [-Winvalid-input]
em++: error: no input files
note that input files without a known suffix are ignored, make sure your input files end with one of: ('.c', '.i', '.cpp', '.cxx', '.cc', '.c++', '.CPP', '.CXX', '.C', '.CC', '.C++', '.ii', '.m', '.mi', '.mm', '.mii', '/dev/null', '.bc', '.o', '.obj', '.lo', '.dylib', '.so', '.a', '.ll', '.h', '.hxx', '.hpp', '.hh', '.H', '.HXX', '.HPP', '.HH')
What am I missing here...?
Change:
em++ CMakeFiles/HelloWasm.dir/src/main.cpp.o CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o -o helloWasm.js
To:
em++ CMakeFiles/HelloWasm.dir/src/main.cpp.o CMakeFiles/HelloWasm.dir/src/my_lib.cpp -o helloWasm.js
Emscripten can't compile my_lib.cpp.o because it's already compiled to machine code (it's an object file). You have to use a .cpp file, not .cpp.o.
I have a suggestion for you.
First, you can put your lib files (my_lib.h and my_lib.cpp) on same directory.
Second, you can create a folder for your applications (HelloWasm) and put your executable codes (main.cpp) in this folder.
Finally, you can create CMakeLists.txt file for each folder.
This is your new directory tree:
.
├── CMakeLists.txt
├── Applications
│ └── HelloWasm
│ ├── CMakeLists.txt
│ └── main.cpp
└── Libraries
├── CMakeLists.txt
├── my_lib.h
└── my_lib.cpp
CMakeLists.txt (the first one) :
cmake_minimum_required(VERSION 3.0)
project(LIBRARYANDAPPS)
#This function is starting build and looking all folders.
function( start_build )
file( GLOB_RECURSE components "${CMAKE_SOURCE_DIR}/*/CMakeLists.txt" )
foreach( component ${components} )
get_filename_component( path ${component} PATH )
add_subdirectory( ${path} )
endforeach( )
endfunction( )
start_build()
CMakeLists.txt (for HelloWasm):
#You can add Emscripten flags like this.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
-std=c++11 --bind \
-s USE_WEBGL2=1 -s FULL_ES3=1 --memory-init-file 0")
find_package(Eigen3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})
include_directories("${CMAKE_SOURCE_DIR}/Libraries")
ADD_EXECUTABLE(HelloWasm main.cpp)
TARGET_LINK_LIBRARIES(HelloWasm MyLib Eigen)
CMakeLists.txt (for Library):
find_package(Eigen3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})
ADD_LIBRARY(MyLib STATIC my_lib.cpp my_lib.h)
TARGET_LINK_LIBRARIES(MyLib Eigen)
With these edits, you can create bigger projects, libraries. And I think more useful like this.
I'm writing code in C++ and I need to read a file, problem is my file structure is a bit complex and I just cannot figure out how to use my ifstream to read it. I think I tried all possible combinations... but it just doesn't work, I guess I'm doing something wrong but I can't figure it out.
Here is a minimal reproduction of my problem.
structure :
.
├── build
├── CMakeLists.txt
└── src
├── file
│ └── test.txt
├── load
│ └── loadfile.hpp
└── main.cpp
CMakeLists.txt :
cmake_minimum_required(VERSION 3.10)
project(BaseProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-O3 -g -std=c++17 -Wall -Wextra -pedantic")
file(GLOB SRC
"src/*.h"
"src/*.hpp"
"src/load/*.h"
"src/load/*.hpp"
"src/load/*.cpp"
)
add_executable(exec ${SRC} src/main.cpp)
main.cpp
#include "load/loadfile.hpp"
int main(){
load();
return 0;
}
test.txt : (not very relevent but meh)
test
loadfile.hpp
#include <fstream>
#include <string>
#include <iostream>
void loadFile(const std::string& file){
std::ifstream i(file, std::ifstream::in);
std::string str;
i >> str;
std::cerr << str;
i.close();
}
void load(){
loadFile("../file/test.txt");
}
output is empty and program finishes normally.
This might help, when you call install your files will be copied to destination specified.
cmake_minimum_required(VERSION 3.10)
project(BaseProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-O3 -g -std=c++17 -Wall -Wextra -pedantic")
set(OUTPUT_DIR "Yours to fill, working dir")
file(GLOB RESOURCES "./file/test.txt")
file(GLOB SRC
"src/*.h"
"src/*.hpp"
"src/load/*.h"
"src/load/*.hpp"
"src/load/*.cpp"
)
add_executable(exec ${SRC} src/main.cpp)
install (FILES ${RESOURCES} DESTINATION ${OUTPUT_DIR})
Answer from Some programmer dude in the comments:
Now might be a good time to learn about the concept of current working
directory. When you run a program, its process will have a current
working directory. If you run from a console or terminal then it's
usually the terminals current directory. Relative paths (paths not
beginning with a /) are always relative to the current working
directory. You need to make sure that the relative path in the program
is valid for the programs current working directory when running.
I'm using CMake with my project and set up a cdash server for continuous/nightly building. Everything works well and by setting up a crontab, we have hourly/nightly build/test results uploaded to our cdash server automatically.
My next step is to add test coverage report to the build. I find the document here https://gitlab.kitware.com/cmake/community/-/wikis/doc/ctest/Coverage but frankly it's a bit far from a practical guide.
Currently I've added the required flag (instead of -fprofile-arcs -ftest-coverage, I find --coverage better), the compilation process generates .gcno files. But then I'm stuck. The command
make NightlyCoverage
doesn't seem to do anything. Could anybody tell me what is the next to do? The result that I want, is by doing make NightlyCoverage, coverage reports are generated and uploaded to cdash server.
I've been using https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake successfully.
Just followed the guidelines: added the files to my CMAKE_MODULE_PATH directory, added
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeModules)
if(CMAKE_COMPILER_IS_GNUCXX)
include(CodeCoverage)
setup_target_for_coverage(${PROJECT_NAME}_coverage ${PROJECT_TEST_NAME} coverage)
endif()
in my CMakeLists.txt. I also added manually gcov as a dependency for my target:
if(CMAKE_COMPILER_IS_GNUCXX)
target_link_libraries(${PROJECT_TEST_NAME} gcov)
endif()
With this, I just type
make my_project_coverage
and I get the html report in the coverage directory of my build tree.
I use gcovr to make a GCC Code Coverage Report without the CodeCoverage.cmake :
$ cd /path/to/your/project
$ mkdir build && cd build && cmake ..
$ make && make test
$ gcovr -r ../ .
I set up my project 'foo' in the following way. Copied the cmake file from the https://github.com/bilke/cmake-modules/blob/master/CodeCoverage.cmake to a subdirectory 'cmake_modules'. In the CMakeLists.txt file after the add_executable(foo ...) I added the following:
if(CMAKE_COMPILER_IS_GNUCXX)
LIST(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake_modules")
include(CodeCoverage)
APPEND_COVERAGE_COMPILER_FLAGS()
set(COVERAGE_LCOV_EXCLUDES 'dir1/*' 'dir2/*') // this is optional if you want to exclude some directory from the report
SETUP_TARGET_FOR_COVERAGE_LCOV(NAME foo_coverage
EXECUTABLE foo
DEPENDENCIES foo)
endif()
After cmake, build the target
make
make foo_coverage
And open the report with index.html file in the foo_coverage folder in the build folder
This answer is the same as #rcomblen's answer but its a bit outdated so I'll share my solution. Here's what I did:
created a toy project
(base) ciaran#DESKTOP-K0APGUV:/mnt/d/CoverageTest$ tree -L 2
.
├── CMakeLists.txt
├── cmake
│ └── CodeCoverage.cmake
├── src
│ ├── domath.cpp
│ ├── domath.h
│ └── testdomath.cpp
└── third_party
└── googletest
Where
// src/domath.h
#ifndef COVERAGETEST_DOMATH_H
#define COVERAGETEST_DOMATH_H
class domath {
public:
int add(int a, int b);
};
#endif //COVERAGETEST_DOMATH_H
and
// src/domath.cpp
#include "domath.h"
int domath::add(int a, int b) {
return a + b;
}
and
// src/testdomath.cpp
#include "gtest/gtest.h"
#include "domath.h"
TEST(DoMathTests, testAdd){
domath m;
int actual = m.add(4, 6);
ASSERT_EQ(10, actual);
}
download googletest and put it under third party directory
copy the gist so kindly shared by the other answers on this thread into cmake/CodeCoverage.cmake
install gcovr. This step is vital, because the other answers on this thread no longer work with the version of gcovr that I already had:
(base) ciaran#DESKTOP-K0APGUV:/mnt/d/CoverageTest$ pip install gcovr
(base) ciaran#DESKTOP-K0APGUV:/mnt/d/CoverageTest$ gcovr --version
gcovr 4.2
(base) ciaran#DESKTOP-K0APGUV:/mnt/d/CoverageTest$ which gcovr
/home/ciaran/miniconda3/bin/gcovr
Note, we need the output of which gcovr for the cmake script.
4) Write a cmake script that create a library, a test executable and use the CodeCoverage.cmake module:
cmake_minimum_required(VERSION 3.15)
project(CoverageTest)
set(CMAKE_CXX_STANDARD 14)
# setup googletest
add_subdirectory(third_party/googletest)
# create our library
add_library(DoMath STATIC src/domath.h src/domath.cpp)
add_dependencies(DoMath gtest gtest_main)
# create the test executable
add_executable(TestDoMath src/testdomath.cpp)
target_include_directories(TestDoMath PRIVATE third_party/googletest/googletest)
target_link_libraries(TestDoMath PRIVATE
DoMath gtest gtest_main)
add_dependencies(TestDoMath DoMath gtest gtest_main)
# now for coverage bits
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
if (CMAKE_COMPILER_IS_GNUCXX)
include(CodeCoverage)
append_coverage_compiler_flags()
# we need to turn off optimization for non-skewed coverage reports
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
# optional excludes - None needed here
# set(COVERAGE_EXCLUDES)
# importantly, set the path to the gcovr executable that you downladed
set(GCOVR_PATH "/home/ciaran/miniconda3/bin/gcovr")
# Works
setup_target_for_coverage_gcovr_xml(
NAME TestDoMathCoverageXml
EXECUTABLE TestDoMath
DEPENDENCIES TestDoMath DoMath
)
# Works
setup_target_for_coverage_gcovr_html(
NAME TestDoMathCoverageHtml
EXECUTABLE TestDoMath
DEPENDENCIES TestDoMath DoMath
)
# This one did not work for me:
setup_target_for_coverage_lcov(
NAME TestDoMathCoverageLcov
EXECUTABLE TestDoMath
DEPENDENCIES TestDoMath DoMath
)
endif ()
And thats it. Now just build the new targets.
Good luck.