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})
Related
My project was building fine with make, until I imported stb, now I get this error
ld: warning: ignoring file /opt/homebrew/lib/libglfw.3.4.dylib, building for macOS-x86_64 but attempting to link with file built for macOS-arm64
Undefined symbols for architecture x86_64:
"_glfwCreateWindow", referenced from:
HelloTriangleApplication::initWindow() in main.cpp.o
"_glfwCreateWindowSurface", referenced from:
HelloTriangleApplication::createSurface() in main.cpp.o
"_glfwDestroyWindow", referenced from:
HelloTriangleApplication::cleanup() in main.cpp.o
"_glfwGetFramebufferSize", referenced from:
HelloTriangleApplication::chooseSwapExtent(VkSurfaceCapabilitiesKHR const&) in main.cpp.o
HelloTriangleApplication::recreateSwapChain() in main.cpp.o
"_glfwGetRequiredInstanceExtensions", referenced from:
HelloTriangleApplication::getRequiredExtensions() in main.cpp.o
"_glfwGetWindowUserPointer", referenced from:
HelloTriangleApplication::framebufferResizeCallback(GLFWwindow*, int, int) in main.cpp.o
"_glfwInit", referenced from:
HelloTriangleApplication::initWindow() in main.cpp.o
"_glfwPollEvents", referenced from:
HelloTriangleApplication::mainLoop() in main.cpp.o
"_glfwSetFramebufferSizeCallback", referenced from:
HelloTriangleApplication::initWindow() in main.cpp.o
"_glfwSetWindowUserPointer", referenced from:
HelloTriangleApplication::initWindow() in main.cpp.o
"_glfwTerminate", referenced from:
HelloTriangleApplication::cleanup() in main.cpp.o
"_glfwWaitEvents", referenced from:
HelloTriangleApplication::recreateSwapChain() in main.cpp.o
"_glfwWindowHint", referenced from:
HelloTriangleApplication::initWindow() in main.cpp.o
"_glfwWindowShouldClose", referenced from:
HelloTriangleApplication::mainLoop() 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[2]: *** [vulkanLearning] Error 1
make[1]: *** [CMakeFiles/vulkanLearning.dir/all] Error 2
make: *** [all] Error 2
It seems like the program is compiling for x86_64, but the libglfw I've installed from homebrew is for aarch64. How do I get it to compile for aarch64?
CMakelists.txt is using C++20, it's a Vulkan proj (Using custom env vars)
cmake_minimum_required (VERSION 3.12)
project(
vulkanLearning VERSION 0.1.0
DESCRIPTION "vulkanLearning"
LANGUAGES CXX
)
# Specify the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build)
# Set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
add_executable(${PROJECT_NAME} main.cpp)
find_package(Vulkan REQUIRED)
find_package(glfw3 3.3 REQUIRED)
if (VULKAN_FOUND)
message(STATUS "Found Vulkan, Including and Linking now")
include_directories(${Vulkan_INCLUDE_DIRS})
target_link_libraries (${PROJECT_NAME} ${Vulkan_LIBRARIES} glfw)
endif (VULKAN_FOUND)
It seems as if CMake is building for x86_64 even though your processor runs on arm64. This is influenced by CMAKE_HOST_SYSTEM_PROCESSOR, so that should be arm64.
Setting the CMAKE_OSX_ARCHITECTURE should override that default so that it will build your application for arm64:
set(CMAKE_OSX_ARCHITECTURES "arm64")
That will force clang (or whatever compiler you're using) to compile for arm64.
I was trying to generate a shared library for my project using cmake, unfortunately I got this error
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in main.cpp.o
"_SDL_Quit", 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)
If I'm building a static library it works. This is my cmake file :
cmake_minimum_required(VERSION 3.4.1)
project(yanthra_console VERSION 0.1 DESCRIPTION "A 3d Game Engine.")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -fexceptions")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo;Release;Debug" CACHE STRING "Build type selections" FORCE)
set(THIRD_PARTY_DIR "../../third-party")
set(MAIN_SOURCE_DIR "../main/src")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/out)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib )
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
include_directories(${THIRD_PARTY_DIR}/SDL/include)
file(GLOB_RECURSE CPP_HEADERS ${MAIN_SOURCE_DIR}/*.hpp)
file(GLOB_RECURSE CPP_SOURCES ${MAIN_SOURCE_DIR}/*.cpp)
add_library(
yanthra
SHARED
${CPP_HEADERS}
${CPP_SOURCES}
)
add_executable(
yanthra_console
${CPP_HEADERS}
${CPP_SOURCES}
)
set_target_properties(
yanthra_console
PROPERTIES
LINK_FLAGS
"-F../Frameworks -framework SDL2 -framework OpenGL"
)
target_link_libraries(yanthra_console PRIVATE yanthra)
I was able to create a static library with executable.Im using Mulit Configuration to build the project.
Looks like a symbol visibility issue.
By default on clang/gcc symbols are hidden.
There is a cppcon talk that talks about this:
https://www.youtube.com/watch?v=m0DwB4OvDXk&list=PL4s9OdsBXD7aXhgqibbEzf8zAM5eiiENs&index=9
Basically either this library doesn't support being built as a shared library.
Or you need to enable that functionality somehow.
Or just force symbol visibility on.
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).