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?
Related
I am using MacBook with M1 and I try to create a project with SFML.
I downloaded a snapshot of SFML with files built for arm64
I verified it using file command
Example:
libsfml-graphics.dylib: Mach-O 64-bit dynamically linked shared library arm64
I placed these files in ~/Library/Frameworks along with extlibs directory content.
I set a CMake flag: -DCMAKE_OSX_ARCHITECTURES=arm64
But I still am unable to run this project.
Error:
[ 50%] Linking CXX executable myProject
Undefined symbols for architecture arm64:
"__ZN2sf6StringC1EPKcRKSt6locale", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
make[3]: *** [myProject] Error 1
make[2]: *** [CMakeFiles/myProject.dir/all] Error 2
make[1]: *** [CMakeFiles/myProject.dir/rule] Error 2
make: *** [myProject] Error 2
This is my whole CMakeLists.txt file:
cmake_minimum_required(VERSION 3.19)
project(myProject)
add_executable(myProject main.cpp)
set(CMAKE_CXX_COMPILER "/opt/homebrew/bin/g++-11" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCMAKE_OSX_ARCHITECTURES=arm64")
include_directories(/usr/local/include)
find_package(SFML 2.5 COMPONENTS system window graphics network audio REQUIRED)
include_directories(${SFML_INCLUDE_DIRS})
target_link_libraries(myProject sfml-system sfml-window sfml-graphics sfml-audio sfml-network)
I also tried to use dylib instead of framework, but it was unsuccessful also. I'd be grateful for any help, I can't figure out what I did wrong.
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})
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()
I am trying to build the hello.c example from http://www.glprogramming.com/red/chapter01.html (look for "Example 1-2").
My CMakeLists.txt is as follows:
cmake_minimum_required (VERSION 2.8)
project (GLUTEX)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
include_directories(${OpenGL_INCLUDE_DIRS})
add_executable (glutex glutex.c)
target_link_libraries (glutex ${OpenGL_LIBRARIES})
target_link_libraries (glutex ${GLUT_LIBRARIES})
The CMake call succeeds in generating the required Makefile. But when I call make, I encounter the following:
Scanning dependencies of target glutex
[100%] Building C object CMakeFiles/glutex.dir/glutex.c.o
Linking C executable glutex
/usr/bin/ld: CMakeFiles/glutex.dir/glutex.c.o: undefined reference to symbol 'glOrtho'
/usr/bin/ld: note: 'glOrtho' is defined in DSO /usr/lib64/libGL.so.1 so try adding it to the linker command line
/usr/lib64/libGL.so.1: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
make[2]: *** [glutex] Error 1
make[1]: *** [CMakeFiles/glutex.dir/all] Error 2
make: *** [all] Error 2
How do I fix this?
Try changing
target_link_libraries (glutex ${OpenGL_LIBRARIES})
to
target_link_libraries (glutex ${OPENGL_LIBRARIES})