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
Related
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.
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'm working on a C++ program which makes use of the dialog library (http://invisible-island.net/dialog/), which is written in C. I've been able to successfully compile and link my application against it on Linux with the following CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(zerus)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Lua REQUIRED)
find_package(Curses REQUIRED)
find_package(Threads REQUIRED)
add_executable(${PROJECT_NAME} main.cpp headers/Utilities.h source/Utilities.cpp headers/config/lua/LuaConfiguration.h source/config/lua/LuaConfiguration.cpp headers/config/PlatformProcessConfiguration.h source/config/PlatformProcessConfiguration.cpp headers/config/PlatformConfiguration.h source/config/PlatformConfiguration.cpp headers/config/PlatformServiceConfiguration.h source/TmuxSessionManager.cpp headers/TmuxSessionManager.h source/PlatformOvermind.cpp headers/PlatformOvermind.h headers/ProcessStartResult.h headers/dialogs/DialogMainMenu.h source/dialogs/DialogMainMenu.cpp source/dialogs/ModdedDialog.cpp headers/dialogs/ModdedDialog.h source/dialogs/DialogServicesChecklist.cpp headers/dialogs/DialogServicesChecklist.h source/ui/UIManager.cpp headers/ui/UIManager.h headers/dialogs/DialogRegionsChecklist.h source/dialogs/DialogRegionsChecklist.cpp source/dialogs/DialogResolveDependenciesYesNo.cpp headers/dialogs/DialogResolveDependenciesYesNo.h source/dialogs/DialogProcessesChecklist.cpp headers/dialogs/DialogProcessesChecklist.h)
target_link_libraries(${PROJECT_NAME} lua curl dialog Threads::Threads)
However, when I try to build the same project on Mac, it compiles but the linker fails with:
Undefined symbols for architecture x86_64:
"_COLOR_PAIRS", referenced from:
_dlg_print_text in libdialog.a(util.o)
_dlg_color_setup in libdialog.a(util.o)
_dlg_color_pair in libdialog.a(util.o)
"_COLS", referenced from:
_dlg_put_backtitle in libdialog.a(util.o)
_dlg_clear in libdialog.a(util.o)
_init_dialog in libdialog.a(util.o)
_dlg_auto_size in libdialog.a(util.o)
_dlg_auto_sizefile in libdialog.a(util.o)
_dlg_ctl_size in libdialog.a(util.o)
_dlg_box_x_ordinate in libdialog.a(util.o)
...
"_LINES", referenced from:
_dlg_clear in libdialog.a(util.o)
_init_dialog in libdialog.a(util.o)
_dlg_print_scrolled in libdialog.a(util.o)
_dlg_auto_size in libdialog.a(util.o)
_dlg_auto_sizefile in libdialog.a(util.o)
_dlg_ctl_size in libdialog.a(util.o)
_dlg_calc_listh in libdialog.a(util.o)
...
"__nc_wacs", referenced from:
_dlg_draw_arrows2 in libdialog.a(arrows.o)
_dlg_draw_scrollbar in libdialog.a(arrows.o)
"_acs_map", referenced from:
_dlg_put_backtitle in libdialog.a(util.o)
_dlg_boxchar in libdialog.a(util.o)
_dlg_print_scrolled in libdialog.a(util.o)
_dlg_draw_box2 in libdialog.a(util.o)
_dlg_asciibox in libdialog.a(util.o)
_dlg_draw_bottom_box2 in libdialog.a(util.o)
_dlg_draw_arrows2 in libdialog.a(arrows.o)
...
"_beep", referenced from:
_dialog_yesno in libdialog.a(yesno.o)
_dlg_beeping in libdialog.a(util.o)
_dlg_checklist in libdialog.a(checklist.o)
_dlg_menu in libdialog.a(menubox.o)
_dlg_edit_string in libdialog.a(inputstr.o)
_mouse_wgetch in libdialog.a(mousewget.o)
_dialog_textbox in libdialog.a(textbox.o)
...
(maybe you meant: _dlg_beeping)
"_cbreak", referenced from:
_init_dialog in libdialog.a(util.o)
...etc.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've tried several approaches to fix this including:
using extern "C" {} to include the dialog header (didn't use on Linux and everything linked correctly).
Changing build file to the following to ensure that CMake was finding the library correctly (and it was).
CMakeLists.txt:
cmake_minimum_required(VERSION 3.8)
project(zerus)
MESSAGE( STATUS "CMAKE_LIBRARY_PATH: " ${CMAKE_LIBRARY_PATH} )
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
find_package(Lua REQUIRED)
find_package(Curses REQUIRED)
find_package(Threads REQUIRED)
find_library(DIALOG_LIB dialog)
message(DIALOG_LIB=${DIALOG_LIB})
add_executable(${PROJECT_NAME} <source files>)
target_link_libraries(${PROJECT_NAME} ${DIALOG_LIB} lua curl Threads::Threads)
Output during CMake's build verified that it was finding the library:
DIALOG_LIB=/usr/local/lib/libdialog.a
Verifying that the static library created using make/make install-full on the Mac was for the correct architecture:
lipo -info /usr/local/lib/libdialog.a
input file /usr/local/lib/libdialog.a is not a fat file
Non-fat file: /usr/local/lib/libdialog.a is architecture: x86_64
Verifying that the object files inside the static library matched the architecture
I'm not sure where to go from here. I get that the linker can't find the symbols, but it seems to be finding the library which includes those symbols. Is this related to the toolchain on Mac and if so, how can it be fixed?
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 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).