I have a linking problem, when I am trying to use filesystem from boost. I copied code from tutorial:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
Perhaps it should work, but I have such an error:
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)", referenced from:
boost::filesystem::file_size(boost::filesystem::path const&) in main.cpp.o
"boost::system::system_category()", referenced from:
___cxx_global_var_init.2 in main.cpp.o
"boost::system::generic_category()", referenced from:
___cxx_global_var_init in main.cpp.o
___cxx_global_var_init.1 in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
My CMakeLists.txt file:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})
find_package(Boost)
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
endif()
What can I improve to compile this code? I use CLion editor on OSX.
Change this line in your CMakeList.txt:
find_package(Boost)
by
find_package(Boost COMPONENTS system filesystem REQUIRED)
And don't forget to link with your target:
target_link_libraries(untitled
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
So your CMakeList.txt will be:
cmake_minimum_required(VERSION 3.7)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(untitled ${SOURCE_FILES})
find_package(Boost COMPONENTS system filesystem REQUIRED)
IF (Boost_FOUND)
include_directories(${Boost_INCLUDE_DIR})
target_link_libraries(untitled
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
)
endif()
Related
I tried to test OpenMP on MacOS with the Apple Clang compiler that comes with Xcode.
Code:
#include <iostream>
#include <cmath>
#include </usr/local/opt/libomp/include/omp.h>
int main() {
std::cout << "Hello, World!" << std::endl;
#pragma omp parallel
{
printf("Hello World! \n");
}
return 0;
}
and this is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
include (FindOpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
message (VERBOSE "OpenML library found")
else()
message (FATAL_ERROR "OpenML library not found (required for multithreading)")
endif ()
add_executable(untitled main.cpp)
This is the compiler's error:
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
Undefined symbols for architecture x86_64:
"___kmpc_fork_call", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
The OpenMP is installed using hombrew libomp I also tried adding environment variable LDFLAGS=-L/usr/local/opt/libomp/lib/ and it does not affect the result
UPDATE: COMPILATION WITH VERBOSE=1:
...
[100%] Linking CXX executable untitled
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/untitled.dir/link.txt --verbose=1
/Library/Developer/CommandLineTools/usr/bin/c++ -Xclang -fopenmp -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names -L/usr/local/opt/libomp/lib/ CMakeFiles/untitled.dir/main.cpp.o -o untitled
Undefined symbols for architecture x86_64:
"___kmpc_fork_call", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled] Error 1
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
make: *** [untitled] Error 2
By using set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}") and using set in other places, you are replacing the flags that CMAKE puts. It is better to append in this case rather than replace. You can use add_compile_options(-Wall) here.
As mentioned by #Tsyvarev, it is perhaps better to use imported library. Here are a few other ways you can use target_link_libraries.
For your particular case, it is best to replace
find_package(OpenMP REQUIRED)
where you currently have include (FindOpenMP). Further, remove the if condition as well. Finally, add the following to the end of your CMakeLists.txt file.
target_link_libraries(
untitled
PRIVATE
OpenMP::OpenMP_CXX
)
Here is a good explanation of the difference between PRIVATE and PUBLIC.
Your final CMakeLists.txt file should look like:
cmake_minimum_required(VERSION 3.19)
project(untitled)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenMP REQUIRED)
add_executable(
untitled
main.cpp
)
target_link_libraries(
untitled
PRIVATE
OpenMP::OpenMP_CXX
)
Thanks to Tsyvarev, for helping me with CMake a few months ago. Further, thanks to the answers that Tsyvarev points toward, which my answer is based on (along with my own testing).
I've faced following exception when i build simple .cpp file that includes c
pcl_visualizer.h. Only included!
Building IDE CLion
[ 50%] Building CXX object CMakeFiles/untitled2.dir/main.cpp.o
[100%] Linking CXX executable untitled2
Undefined symbols for architecture x86_64:
"vtkDebugLeaksManager::vtkDebugLeaksManager()", referenced from:
___cxx_global_var_init.3 in main.cpp.o
"vtkDebugLeaksManager::~vtkDebugLeaksManager()", referenced from:
___cxx_global_var_init.3 in main.cpp.o
"vtkObjectFactoryRegistryCleanup::vtkObjectFactoryRegistryCleanup()", referenced from:
___cxx_global_var_init.4 in main.cpp.o
"vtkObjectFactoryRegistryCleanup::~vtkObjectFactoryRegistryCleanup()", referenced from:
___cxx_global_var_init.4 in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [untitled2] Error 1
make[2]: *** [CMakeFiles/untitled2.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled2.dir/rule] Error 2
Here is my CMakeLists.txt configuration
cmake_minimum_required(VERSION 3.14)
project(untitled2)
include_directories(
"/usr/local/include/pcl-1.9"
"/usr/local/include/eigen3"
"/usr/local/include/vtk/8.2.0"
"/usr/local/include/flann/"
"/usr/local/Cellar/boost/1.71.0/include"
"/usr/local/Cellar/vtk/8.2.0_3/include/vtk-8.2"
)
link_directories(
"/usr/local/lib/"
)
set(CMAKE_CXX_STANDARD 17)
add_executable(untitled2 main.cpp)
The CMakeLists.txt doesn't need to be so verbose, fragile. Moreover, you are not linking the libraries, just specifying the paths to search for. Related
Docs for link_directories As it says in the docs, this is bad practice since find_package is supposed to do all this for you.
You need to specify a target_link_libraries in order to find the symbols.
I use the following (link to repo):
cmake_minimum_required(VERSION 3.5)
project(pcl_cmake_minimum)
find_package(PCL COMPONENTS common)
add_executable(pcl_demo main.cpp)
target_link_libraries(pcl_demo ${PCL_LIBRARIES})
My CMakeLists.txt is here.
cmake_minimum_required(VERSION 3.9)
project(iemoji-lab)
set(CMAKE_CXX_STANDARD 14)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
add_executable(
util
src/util/util.cpp
)
target_link_libraries(
util
avformat
)
After I build, I got util executable file. As you see, I have to link util with avformat which is in ffmepg.
Now I want to build a .so file named libiemoji.
I have already tried like this.
cmake_minimum_required(VERSION 3.9)
project(iemoji-lab)
set(CMAKE_CXX_STANDARD 14)
add_library(
libiemoji
SHARED
src/util/util.cpp
)
But I got error.
Undefined symbols for architecture x86_64:
"_av_register_all", referenced from:
GetFrameCount(char const*) in util.cpp.o
"_avformat_alloc_context", referenced from:
GetFrameCount(char const*) in util.cpp.o
"_avformat_close_input", referenced from:
GetFrameCount(char const*) in util.cpp.o
"_avformat_free_context", referenced from:
GetFrameCount(char const*) in util.cpp.o
"_avformat_open_input", referenced from:
GetFrameCount(char const*) in util.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
gmake[2]: * [CMakeFiles/libiemoji.dir/build.make:95: liblibiemoji.dylib] Error 1
gmake[1]: * [CMakeFiles/Makefile2:68: CMakeFiles/libiemoji.dir/all] Error 2
gmake: *** [Makefile:84: all] Error 2
So how can generate .so library?
EDIT
After #Mario point, I got solution for no error.
cmake_minimum_required(VERSION 3.9)
project(iemoji-lab)
set(CMAKE_CXX_STANDARD 14)
SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
add_library(
iemoji
SHARED
src/util/util.cpp
)
target_link_libraries(
iemoji
avformat
)
Now I got a file libiemoji.dyib. I am confused by it because I want to generate a file with extension .so.
You've got everything right, you just forgot linking the library as well. Copy target_link_libraries() and you should be fine.
The sub-project should link libraries, similar to executable project; because of dependencies; you can look at CMake tutorial
I have the following CMake file on osx using IntelliJ/CLion:
cmake_minimum_required(VERSION 3.9)
project(GrepUtil)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1z -fsanitize=thread -Wall")
set(SOURCE_FILES main.cpp)
add_executable(GrepUtil ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} stdc++fs)
While building it, I am getting the following errors:
[ 50%] Building CXX object CMakeFiles/GrepUtil.dir/main.cpp.o
[100%] Linking CXX executable GrepUtil
Undefined symbols for architecture x86_64:
"___tsan_atomic32_fetch_add", referenced from:
__gnu_cxx::__exchange_and_add(int volatile*, int) in main.cpp.o
"___tsan_atomic8_load", referenced from:
...
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[3]: *** [GrepUtil] Error 1
make[2]: *** [CMakeFiles/GrepUtil.dir/all] Error 2
make[1]: *** [CMakeFiles/GrepUtil.dir/rule] Error 2
I am using g++-7 (7.1.0) compiler. Program builds fine without the sanitize option. Is tsan supported with/on Sierra?
I would like to use the BamTools library for a project. I am using CMake ExternalProject_Add to do this. CMake clones and compiles BamTools GitHub repo fine, but no matter what I try I can't get it to link properly. Does anyone have any ideas?
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(myProject)
include(ExternalProject)
set( CMAKE_C_FLAGS "-Wall -g")
set(BAMTOOLS_ROOT ${CMAKE_CURRENT_BINARY_DIR}/external/bamtools)
set(BAMTOOLS_INCLUDE_DIRS ${BAMTOOLS_ROOT}/include/bamtools)
set(BAMTOOLS_LIBRARIES ${BAMTOOLS_ROOT}/lib/bamtools/libbamtools.a)
set(bamtools_INSTALL_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/bamtools")
set(bamtools_CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=${bamtools_INSTALL_DIR})
include_directories(${BAMTOOLS_INCLUDE_DIRS})
ExternalProject_Add(bamtools
PREFIX ${BAMTOOLS_ROOT}
GIT_REPOSITORY https://github.com/pezmaster31/bamtools
BINARY_DIR ${BAMTOOLS_ROOT}
INSTALL_DIR ${BAMTOOLS_ROOT}
CMAKE_ARGS ${bamtools_CMAKE_ARGS}
)
add_subdirectory(src)
My simple main.cpp file exists in a src folder in the same directory as the CMakeLists.txt
#include <iostream>
#include <string>
#include <api/BamReader.h>
using namespace BamTools;
int main(){
BamReader reader =BamReader();
reader.Close();
};
I get the following error
Linking CXX executable project
Undefined symbols for architecture x86_64:
"_crc32", referenced from:
BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
"_deflate", referenced from:
BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
"_deflateEnd", referenced from:
BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
"_deflateInit2_", referenced from:
BamTools::Internal::BgzfStream::DeflateBlock(int) in libbamtools.a(BgzfStream_p.cpp.o)
"_inflate", referenced from:
BamTools::Internal::BgzfStream::InflateBlock(unsigned long const&) in libbamtools.a(BgzfStream_p.cpp.o)
"_inflateEnd", referenced from:
BamTools::Internal::BgzfStream::InflateBlock(unsigned long const&) in libbamtools.a(BgzfStream_p.cpp.o)
"_inflateInit2_", referenced from:
BamTools::Internal::BgzfStream::InflateBlock(unsigned long const&) in libbamtools.a(BgzfStream_p.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [src/project] Error 1
make[1]: *** [src/CMakeFiles/project.dir/all] Error 2
make: *** [all] Error 2
Like the other answer said it was not linking ZLib properly. Making the following changes will resolve the issue. Instead of the add_subdirectory(src) add
find_package(ZLIB REQUIRED)
add_library(libbamtools STATIC IMPORTED)
set_target_properties(libbamtools PROPERTIES IMPORTED_LOCATION ${BAMTOOLS_LIBRARIES}/libbamtools.a)
add_dependencies(libbamtools bamtools)
add_executable(myProject src/main.cpp)
include_directories(${BAMTOOLS_INCLUDE_DIRS})
target_link_libraries(myProject libbamtools ${ZLIB_LIBRARIES})
Those are functions from zlib, I believe. You may need to explicitly link with zlib. Try adding find_package(ZLIB REQUIRED).