I'm trying to set up a project project in Xcode with allegro. I installed allegro5 using homebrew. My CMakeLists.txt is as below:
set(SOURCE_FILES "main.cpp")
add_executable(core ${SOURCE_FILES})
if(WIN32)
# TODO.
else(APPLE)
set(ALLEGRO_INCLUDE "/usr/local/include")
set(ALLEGRO_LIB "/usr/local/lib")
set(ALLEGRO_DYLIB, "/usr/local/lib/*.dylib")
set(ALLEGRO_LINK_FLAGS "-lallegro -lallegro_main")
endif()
include_directories(${ALLEGRO_INCLUDE})
link_directories(${ALLEGRO_LIB})
file(GLOB LIBRARIES ${ALLEGRO_DYLIB})
target_link_libraries(core ${LIBRARIES} ${ALLEGRO_LINK_FLAGS})
However, I keep getting the error: ld: library not found for -lallegro
EDIT:
Edited CMakeLists.txt file:
set(SOURCE_FILES "main.cpp")
add_executable(core ${SOURCE_FILES})
if(WIN32)
# TODO.
else(APPLE)
set(ALLEGRO_INCLUDE "/usr/local/include")
set(ALLEGRO_LIB "/usr/local/lib")
set(ALLEGRO_DYLIB, "/usr/local/lib/*.dylib")
endif()
include_directories(${ALLEGRO_INCLUDE})
link_directories(${ALLEGRO_LIB})
file(GLOB LIBRARIES ${ALLEGRO_DYLIB})
target_link_libraries(core ${LIBRARIES} ${ALLEGRO_DYLIB})
And now I'm getting the error:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: __al_mangled_main)
ld: symbol(s) not found for architecture x86_64
You misunderstood target_link_libraries, you don't set the link flags here, but you indicate the list of libraries you want to target, so that would be:
target_link_libraries(core ${LIBRARIES} ${ALLEGRO_DYLIB})
Otherwise you will get flags like -l-lallegro.
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'm trying to get a C library called tree-sitter working with my C++ project.
This is what my project structure looks like:
IDEProject
cmake-build-debug
Headers
Sources
tree-sitter (The library I want to include)
tree-sitter-json (The library that provides a parser I need to use with tree-sitter)
CMakeLists.txt
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.1.0)
project(IDEProject)
set (CMAKE_PREFIX_PATH "/Users/Chloe/Qt/5.12.0/clang_64")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Find includes in corresponding build directories
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test)
include_directories(Headers)
include_directories(Sources)
file(GLOB project_headers Headers/*.h)
file(GLOB project_sources Sources/*.cpp)
file(GLOB project_forms Forms/*.ui)
# qt_wrap_cpp(project_headers_wrapped ${project_headers})
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
# set(CMAKE_AUTOUIC ON)
add_executable(IDEProject ${project_headers} ${project_sources} ${project_forms})
# Use the Widgets module from Qt 5
target_link_libraries(IDEProject PUBLIC Qt5::Core Qt5::Widgets Qt5::Gui)
add_library(tree-lib tree-sitter/lib/src/lib.c)
include_directories(
tree-sitter/lib/src
tree-sitter/lib/include
tree-sitter/lib/utf8proc
)
target_link_libraries(IDEProject PRIVATE tree-lib)
After compiling that, I get this error:
Undefined symbols for architecture x86_64:
"tree_sitter_json()", referenced from:
PlainTextEdit::onTextChanged() in TextEditor.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)
The code that my program tries to run is this:
TSLanguage *tree_sitter_json();
TSParser *parser = ts_parser_new();
ts_parser_set_language(parser, tree_sitter_json());
NOTE: All the info regarding tree-sitter is taken from this website: https://tree-sitter.github.io/tree-sitter/using-parsers#an-example-program
Help would really really be appreciated since I've been trying to solve this problem for over 2 days now
I'm relatively new to C++, and I'm trying to compile a project using CMake with the following #include:
#include <boost/test/unit_test.hpp>
I get the following error
undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_log_t::instance()", referenced from:
___cxx_global_var_init in functions1.cpp.o
___cxx_global_var_init in functions2.cpp.o
___cxx_global_var_init in main.cpp.o
ld: symbol(s) not found for architecture x86_64
I'm pretty sure this is something to do with my CMakeLists.txt, so here it is:
cmake_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
include_directories(/usr/local/include/)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")
find_package(Boost COMPONENTS filesystem system unit_test_framework REQUIRED)
#find_package(Boost 1.69.0)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories (${Boost_INCLUDE_DIRS})
I'm on OSX Mojave, and installing with brew install boost. I'm aware there are several posts out there that report very similar issues, but none of the solutions seem to work for me.
Edit:
Have adjusted my CMakeLists to the following based on Guillaume's suggestion below.
make_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")
find_package(Boost COMPONENTS filesystem system test REQUIRED)
target_include_directories(MyProject PUBLIC ".")
target_link_libraries(MyProject PUBLIC
Boost::filesystem Boost::system Boost::test)
I understand that this is, in principle, better, but it's giving me:
Unable to find the requested Boost libraries.
Boost version: 1.69.0
Boost include path: /usr/local/Cellar/boost/1.69.0_2/include
Could not find the following Boost libraries:
boost_test
Some (but not all) of the required Boost libraries were found. You may need to install these additional Boost libraries. Alternatively, set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.
Edit 2:
Have tried edits and upgrading to boost 1.7, but still have:
Could not find a package configuration file provided by "boost_test"
(requested version 1.70.0) with any of the following names:
boost_testConfig.cmake
boost_test-config.cmake
Add the installation prefix of "boost_test" to CMAKE_PREFIX_PATH or set
"boost_test_DIR" to a directory containing one of the above files. If
"boost_test" provides a separate development package or SDK, be sure it
has been installed.
You have to link properly to boost instead of adding include directory flags.
Linking libraries in cmake will apply all required property to use boost onto your targets.
First off, don't do that:
include_directories(.)
include_directories(/usr/local/include/)
This is asking for link error. It will enable you to use headers of libraries you don't link properly to. This will cause linking errors, even within your own project.
cmake_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
list(APPEND CMAKE_PREFIX_PATH "/usr/local/Cellar/boost/1.69.0_2")
set(Boost_ADDITIONAL_VERSIONS "1.69.0" "1.69")
find_package(Boost COMPONENTS filesystem system test REQUIRED)
# Not needed
#if(NOT Boost_FOUND)
# message(FATAL_ERROR "Could not find boost!")
#endif()
# include_directories (${Boost_INCLUDE_DIRS})
target_include_directories(MyProject PUBLIC ".")
# adds include directories, definitions and link libraries
target_link_libraries(MyProject PUBLIC
Boost::filesystem Boost::system Boost::test
)
I am new to C++ and external dependencies.
Really don't know how to fix this compile error...
Error:
Undefined symbols for architecture x86_64:
"uS::TLS::Context::~Context()", referenced from:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project( opencv )
add_definitions(
-std=c++11
)
find_package(OpenCV REQUIRED)
find_package(ZLIB REQUIRED)
include_directories(${ZLIB_INCLUDE_DIRS})
find_package(OpenSSL REQUIRED)
include_directories(${OPENSSL_INCLUDE_DIR})
add_executable( opencv main.cpp Camera.cpp Camera.h Communication.cpp Communication.h)
target_link_libraries( opencv
${OpenCV_LIBS}
${OPENSSL_LIBRARIES}
${ZLIB_LIBRARIES}
)
I'm currently trying to compile a program that uses Boost.Filesystem on macOS High Sierra 10.13.4. I'm also using gcc 7.3 to compile, which I manually installed using Homebrew. The program will compile, but then throws the following error during linking
Undefined symbols for architecture x86_64:
"boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)", referenced from:
boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, boost::filesystem::path&>::type boost::filesystem::path::operator=<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&) in world.cc.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I'm using cmake to build, and my CMakeLists.txt file looks like this
cmake_minimum_required (VERSION 3.0)
MESSAGE(STATUS "Compiler: " ${CMAKE_CXX_COMPILER})
# variables for CMAKE
set(PROJECT sealab)
set(BINARY ${PROJECT}.out)
set(SRC_DIR src/)
set(INC_DIR inc/)
FILE(GLOB_RECURSE SRC ${SRC_DIR}/*.cc)
# library include dirs
set(IMGUI_INC libs/imgui/)
set(SPDLOG_INC libs/spdlog/include/)
set(JSON_INC libs/json/include/)
set(
LIB_INC
${IMGUI_INC}
${SPDLOG_INC}
${JSON_INC}
${GLM_INC}
)
# library src files
FILE(GLOB IMGUI_SRC ${IMGUI_INC}*.cpp)
FILE(GLOB SPDLOG_SRC ${SPDLOG_INC}*.cpp)
set(
LIB_SRC
${IMGUI_SRC}
${SPDLOG_SRC}
)
project(${PROJECT})
# version number
set(${PROJECT}_VERSION_MAJOR 0)
set(${PROJECT}_VERSION_MINOR 1)
# Compilation Database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-local-typedefs")
# threads
set(THREADS_PREFER_PTHREAD_FLAG ON)
# adding packages
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 3.2 REQUIRED)
find_package(Threads REQUIRED)
find_package(Assimp REQUIRED)
find_package(glm REQUIRED)
find_package(Boost COMPONENTS system filesystem REQUIRED)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
# adding directories and source files
include_directories(
${OPENGL_INCLUDE_DIRS}
${GLEW_INCLUDE_DIRS}
${ASSIMP_INCLUDE_DIRS}
${GLM_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
include_directories(${INC_DIR} ${LIB_INC})
link_directories(${INC_DIR} ${LIB_INC})
# adding source files
add_executable(${BINARY} ${SRC} ${LIB_SRC})
set_target_properties(${BINARY} PROPERTIES COTIRE_UNITY_TARGET_NAME "unity")
# linking libraries
target_link_libraries(
${BINARY}
OpenGL::GL
${GLEW_LIBRARIES}
Threads::Threads
${ASSIMP_LIBRARIES}
glfw
glm
${Boost_LIBRARIES}
)
I'm using the command cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX . to generate the Makefile, and the environment variables $CC and $CXX are set to the versions of gcc and g++ created by Homebrew.
I've looked at several other stackoverflow answers where people have the same error, and all of those are the result of incorrect linking. I'm linking in the same way that the correct answers say to do, yet the error still persists. Am I still linking it incorrectly or is there something else going on here?
So I found out what was going on. This made me realized that maybe the bottled homebrew Boost 1.66.0 library wasn't built with GCC. I switched the compiler to clang, and now it compiles just fine.