Cannot properly link the libraries in CMakeLists - cmake

I have some troubles with linking the source files (I have added them as Libraries in my CMakeLists) with my main file. Here is my CMakeLists:
cmake_minimum_required(VERSION 2.8.3)
project(ros_bridge)
set(${PROJECT_NAME}_CATKIN_COMPONENTS
nav_msgs
roscpp
sensor_msgs
std_msgs
)
add_compile_options(-std=c++14)
find_package(catkin REQUIRED COMPONENTS ${${PROJECT_NAME}_CATKIN_COMPONENTS})
find_package(RapidJSON REQUIRED)
catkin_package(
INCLUDE_DIRS
include
include/aws_iot_sdk
include/common
include/network
CATKIN_DEPENDS ${${PROJECT_NAME}_CATKIN_COMPONENTS}
)
include_directories(
include
include/aws_iot_sdk
${catkin_INCLUDE_DIRS}
)
## Libraries
add_library(ros_client src/ros_client.cpp)
add_library(ConfigCommon include/common/ConfigCommon.cpp)
add_library(OpenSSL include/network/OpenSSL/OpenSSLConnection.cpp)
add_library(WebSocket include/network/WebSocket/WebSocketConnection.cpp)
add_executable(ros_bridge_node src/main.cpp)
## Lib links
target_link_libraries(ros_client ConfigCommon ${catkin_LIBRARIES} ${RapidJSON_LIBRARIES} OpenSSL WebSocket)
target_link_libraries(ConfigCommon ${catkin_LIBRARIES} ${RapidJSON_LIBRARIES} OpenSSL WebSocket)
target_link_libraries(ros_bridge_node ${catkin_LIBRARIES} ros_client ConfigCommon OpenSSL WebSocket ${RapidJSON_LIBRARIES})
So when the compiler tries to link my main file (which is ros_bridge_node) I get the error with the linkage:
ros_client.cpp:(.text+0x605): undefined reference to `awsiotsdk::util::Logging::GetLogSystem()'
ros_client.cpp:(.text+0x66d): undefined reference to `awsiotsdk::ResponseHelper::ToString[abi:cxx11](awsiotsdk::ResponseCode)'
CMakeFiles/ros_bridge_node.dir/src/ros_client.cpp.o: In function `ros_bridge::RosClient::RunPublish(int&)':
ros_client.cpp:(.text+0x8d9): undefined reference to `awsiotsdk::Utf8String::Create(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `BIO_push'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `awsiotsdk::NetworkConnection::Read(std::vector<unsigned char, std::allocator<unsigned char> >&, unsigned long, unsigned long, unsigned long&)'
/home/abyl/master-wheel/devel/lib/libConfigCommon.so: undefined reference to `awsiotsdk::util::JsonParser::InitializeFromJsonFile(rapidjson::GenericDocument<rapidjson::UTF8<char>, rapidjson::MemoryPoolAllocator<rapidjson::CrtAllocator>, rapidjson::CrtAllocator>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `SHA1'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `BIO_f_base64'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `awsiotsdk::network::OpenSSLConnection::IsPhysicalLayerConnected()'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `BIO_set_flags'
/home/abyl/master-wheel/devel/lib/libWebSocket.so: undefined reference to `BIO_new'
So how can I properly link the libraries against my Main file?
EDIT:
I have set the object files of aws_iot_sdk like this:
set(${aws_sdk} /home/ren/aws-iot-device-sdk-cpp/build/CMakeFiles/aws-iot-sdk-cpp.dir/src)
set_source_files_properties(
${aws_sdk_path}
PROPERTIES
EXTERNAL_OBJECT true
GENERATED true
)
And tried to link that against my main file:
target_link_libraries(ros_bridge_node ros_client ConfigCommon Open WebSocket wslay ${RapidJSON_LIBRARIES} ${catkin_LIBRARIES} -lssl ${aws_sdk})
But still getting the same linkage problem. May be I am linking the object files wrong? (Now all the linkage problem is from the aws_iot_sdk)

Related

Link errors when using CMake and VCPKG with GoogleTest

As a bit of background, I have a project that I have been developing with Visual Studio and VCPKG manifest mode for some time, it contains one static library project and one unit test project. Everything has been working correctly. I'm now trying to migrate this solution to use CMake, this is my first time using CMake.
With CMake the VCPKG dependencies install correctly, and both the static library and unit tests compile, however it fails on the linking step with a large number of linker errors all related to the GoogleTest library. Here is the first, as an example:
CppSlippiTest.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl testing::internal::FormatMatcherDescription(bool,char const *,class std::vector<char const*,class std::allocator<char const *> > const &,class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > const &)" (?FormatMatcherDescription#internal#testing##YA?AV$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_NPEBDAEBV?$vector#PEBDV$allocator#PEBD#std###4#AEBV?$vector#V?$basic_string#DU?$char_traits#D#std##V$allocator#D#2##std##V?$allocator#V?$basic_string#DU$char_traits#D#std##V?$allocator#D#2##std###2##4##Z) referenced in function "private: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl cpp_slippi::MatchOptionalMatcherP2<class testing::internal::Eq2Matcher,class std::optional<unsigned char> >::gmock_Impl<class std::optional<unsigned char> const &>::FormatDescription(bool)const " (?FormatDescription#$gmock_Impl#AEBV?$optional#E#std###$MatchOptionalMatcherP2#VEq2Matcher#internal#testing##V?$optional#E#std###cpp_slippi##AEBA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_N#Z) [C:\Users\Derek\Projects\CppSlippi\build\Test.vcxproj]
There are 36 more of these.
Here is my CMakeLists.txt, slightly abridged for clarity:
cmake_minimum_required(VERSION 3.12...3.24)
# Must be before project()
set(VCPKG_TARGET_TRIPLET x64-windows-static)
project(CppSlippi
VERSION 1.0
DESCRIPTION "Slippi replay file parsing library for C++."
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(GTest CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
add_library(CppSlippi STATIC
CppSlippi/src/CppSlippi.cpp
CppSlippi/src/CppSlippi.h
...)
target_include_directories(CppSlippi PUBLIC CppSlippi/src)
target_compile_features(CppSlippi PUBLIC cxx_std_20)
target_compile_options(CppSlippi PUBLIC /MTd)
set_target_properties(CppSlippi PROPERTIES CXX_EXTENSIONS OFF)
target_link_libraries(CppSlippi PUBLIC nlohmann_json::nlohmann_json)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
include(CTest)
include(GoogleTest)
endif()
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
add_executable(Test
Test/src/CppSlippiTest.cpp
...)
target_include_directories(Test PUBLIC Test/src)
target_compile_features(Test PUBLIC cxx_std_20)
target_compile_options(Test PRIVATE /bigobj /MTd)
set_target_properties(Test PROPERTIES CXX_EXTENSIONS OFF)
target_link_libraries(Test PUBLIC
CppSlippi
nlohmann_json::nlohmann_json
GTest::gtest_main)
gtest_discover_tests(Test)
endif()
Here is my VCPKG manifest:
{
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
"name": "cpp-slippi",
"version": "1.0.0",
"description": "C++ Slippi replay parser.",
"builtin-baseline": "68b7fec22eb5fd9c0236b1e42b3c0deb8e771b37",
"dependencies": [
"gtest",
"nlohmann-json"
],
"supports": "windows"
}
And to build this I am running:
cmake --build build --target Test
I turned on --verbose to get more information, and this is the link command that CMake is running:
Link:
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.34.31933\bin\HostX64\x64\link.exe /ERRORREPO
RT:QUEUE /OUT:"C:\Users\Derek\Projects\CppSlippi\build\Debug\Test.exe" /INCREMENTAL /ILK:"Test.dir\Debug\Test.ilk" /N
OLOGO /NATVIS:"C:\Users\Derek\Projects\CppSlippi\build\vcpkg_installed\x64-windows-static\share\nlohmann_json\nlohman
n_json.natvis" Debug\CppSlippi.lib "vcpkg_installed\x64-windows-static\debug\lib\manual-link\gtest_main.lib" "vcpkg_i
nstalled\x64-windows-static\debug\lib\gtest.lib" kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib
oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifes
t:embed /DEBUG /PDB:"C:/Users/Derek/Projects/CppSlippi/build/Debug/Test.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE
/NXCOMPAT /IMPLIB:"C:/Users/Derek/Projects/CppSlippi/build/Debug/Test.lib" /MACHINE:X64 /machine:x64 Test.dir\Debug
\CppSlippiTest.obj ...
Note the presence of gtest_main.lib and gtest.lib, these are the libraries that I believe should include the missing functions. I have checked that these files are present at the locations shown.
I know that in Visual Studio using the GoogleTest main requires adding an AdditionalDependency manually, but from all the instructions I can find this should not be necessary in CMake and the .lib is already in the command line. I did try using target_link_directories anyways, but this did not help.
At this point I am baffled and searching on Google and Stack Overflow has failed to turn up any help.
You link only one of 4 GoogleTest libraries to your app
target_link_libraries(Test PUBLIC
CppSlippi
nlohmann_json::nlohmann_json
GTest::gtest_main)
Depending on the application needs, it should be linked to at least one more library
target_link_libraries(Test PUBLIC
CppSlippi
nlohmann_json::nlohmann_json
GTest::gtest_main
GTest::gtest)
Or
target_link_libraries(Test PUBLIC
CppSlippi
nlohmann_json::nlohmann_json
GTest::gmock_main
GTest::gmock
GTest::gtest)

Undefined reference to klee when building s2e

I'm trying to start a new prject and build s2e in a new directory. But at arounf the 100% mark, it gives me an undefined reference error. The relevant part (imo) is this:
[ 92%] Linking CXX executable ../ExprTest
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::setModule(llvm::Module*, klee::Interpreter::ModuleOptions const&, bool)':
~/s2e_projects/source/s2e/klee/include/klee/Internal/Module/KModule.h:189: undefined reference to `klee::KModule::KModule(llvm::Module*)'
My directory structure is such:
|-s2e-env
|-s2e_projects
|-venv
I followed the steps from here: http://s2e.systems/docs/s2e-env.html I followed all the steps upto s2e build which is where this problem occurs.
For reference, the (truncated) output which I got from doing s2e build &> log:
[ 88%] Built target kleeModule
make[3]: Entering directory '~/s2e_projects/build/klee-release'
make[3]: Entering directory '~/s2e_projects/build/klee-release'
make[3]: Entering directory '~/s2e_projects/build/klee-release'
make[3]: Leaving directory '~/s2e_projects/build/klee-release'
make[3]: Leaving directory '~/s2e_projects/build/klee-release'
make[3]: Leaving directory '~/s2e_projects/build/klee-release'
make[3]: Entering directory '~/s2e_projects/build/klee-release'
make[3]: Entering directory '~/s2e_projects/build/klee-release'
make[3]: Entering directory '~/s2e_projects/build/klee-release'
[ 91%] Linking CXX executable ../ADTTest
[ 91%] Linking CXX executable ../UtilsTest
[ 92%] Linking CXX executable ../ExprTest
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::setModule(llvm::Module*, klee::Interpreter::ModuleOptions const&, bool)':
~/s2e_projects/source/s2e/klee/include/klee/Internal/Module/KModule.h:189: undefined reference to `klee::KModule::KModule(llvm::Module*)'
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::setModule(llvm::Module*, klee::Interpreter::ModuleOptions const&, bool)':
~/s2e_projects/build/llvm-10.0.0.src/include/llvm/IR/DataLayout.h:393: undefined reference to `llvm::DataLayout::getPointerSize(unsigned int) const'
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::setModule(llvm::Module*, klee::Interpreter::ModuleOptions const&, bool)':
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:107: undefined reference to `klee::KModule::linkLibraries(klee::Interpreter::ModuleOptions const&)'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:108: undefined reference to `klee::KModule::buildShadowStructures()'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:110: undefined reference to `klee::KModule::prepare(klee::Interpreter::ModuleOptions const&, klee::InterpreterHandler*)'
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::initializeGlobalObject(klee::ExecutionState&, boost::intrusive_ptr<klee::ObjectState> const&, llvm::Constant const*, unsigned int)':
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:150: undefined reference to `llvm::ConstantDataSequential::getNumElements() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:151: undefined reference to `llvm::ConstantDataSequential::getElementAsConstant(unsigned int) const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:153: undefined reference to `llvm::DataLayout::getStructLayout(llvm::StructType*) const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:158: undefined reference to `klee::KModule::evalConstant(std::unordered_map<llvm::GlobalValue const*, klee::ref<klee::ConstantExpr>, std::hash<llvm::GlobalValue const*>, std::equal_to<llvm::GlobalValue const*>, std::allocator<std::pair<llvm::GlobalValue const* const, klee::ref<klee::ConstantExpr> > > > const&, llvm::Constant const*, klee::KInstruction const*)'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:163: undefined reference to `klee::ConstantExpr::ZExt(unsigned int)'
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::initializeGlobals(klee::ExecutionState&)':
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:200: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:241: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:247: undefined reference to `llvm::GlobalValue::isDeclaration() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:258: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:260: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:262: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:268: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:281: undefined reference to `llvm::Value::getName() const'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:306: undefined reference to `klee::KModule::evalConstant(std::unordered_map<llvm::GlobalValue const*, klee::ref<klee::ConstantExpr>, std::hash<llvm::GlobalValue const*>, std::equal_to<llvm::GlobalValue const*>, std::allocator<std::pair<llvm::GlobalValue const* const, klee::ref<klee::ConstantExpr> > > > const&, llvm::Constant const*, klee::KInstruction const*)'
~/s2e_projects/source/s2e/klee/lib/Core/Executor.cpp:311: undefined reference to `llvm::Value::getName() const'
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::initializeGlobals(klee::ExecutionState&)':
~/s2e_projects/build/llvm-10.0.0.src/include/llvm/IR/GlobalVariable.h:92: undefined reference to `llvm::GlobalValue::isDeclaration() const'
../../lib/libkleeCore.a(Executor.cpp.o): In function `klee::Executor::initializeGlobals(klee::ExecutionState&)':
...
I had the exact same error and solved it with:
sudo apt install gcc-9 g++-9
Also check to make sure clang selects the installed gcc-9 toolchain:
$ PATH_TO_S2E/install/bin/clang++ -v
...
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/9
...
As for why a compatible gcc toolchain is needed, even though clang is used to compile S2E:
Why clang selects a gcc installation?

Linking C++ on Rust: undefined reference to `operator delete(void*)' and `__gxx_personality_v0', libstdc++ missing?

I'm trying to build a cpp cmake project and link to my Rust project.
cmake_minimum_required(VERSION 3.0)
set (CMAKE_CXX_STANDARD 17)
project(ZLMediaKit_LIB CXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
set(ENABLE_TESTS OFF FORCE)
option(ENABLE_TESTS OFF)
set(ENABLE_OPENSSL FALSE FORCE)
option(ENABLE_OPENSSL OFF)
add_subdirectory(ZLMediaKit)
add_library(libzlmediakit_cpp_interface STATIC interface.cpp)
target_include_directories(libzlmediakit_cpp_interface PUBLIC
.
${CMAKE_CURRENT_SOURCE_DIR}/ZLMediaKit/src
${CMAKE_CURRENT_SOURCE_DIR}/ZLMediaKit/3rdpart/ZLToolKit/src)
target_link_libraries(libzlmediakit_cpp_interface zlmediakit zltoolkit mpeg mov flv libstdc++)
install(TARGETS libzlmediakit_cpp_interface DESTINATION .)
Here's my build.rs:
extern crate cmake;
use cmake::Config;
fn main()
{
let dst = Config::new("zlmediakit_lib").build();
println!("cargo:rustc-link-search=native={}", dst.display());
println!("cargo:rustc-link-lib=static=libzlmediakit_cpp_interface");
}
But I'm getting undefined reference errors:
ong)':
/usr/include/c++/9/ext/new_allocator.h:128: undefined reference to `operator delete(void*)'
/usr/bin/ld: /home/dev/orwell/liborwell_rust/zlmediakit_rust/target/debug/build/zlmediakit_rust-499cc82f14515635/out/liblibzlmediakit_cpp_interface.a(interface.cpp.o): in function `ZLRTSPClient::~ZLRTSPClient()':
/home/dev/orwell/liborwell_rust/zlmediakit_rust/zlmediakit_lib/ZLRTSPClient.h:14: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/usr/bin/ld: /home/dev/orwell/liborwell_rust/zlmediakit_rust/target/debug/build/zlmediakit_rust-499cc82f14515635/out/liblibzlmediakit_cpp_interface.a(interface.cpp.o): in function `std::default_delete<ZLRTSPClient>::operator()(ZLRTSPClient*) const':
/usr/include/c++/9/bits/unique_ptr.h:81: undefined reference to `operator delete(void*, unsigned long)'
/usr/bin/ld: /home/dev/orwell/liborwell_rust/zlmediakit_rust/target/debug/build/zlmediakit_rust-499cc82f14515635/out/liblibzlmediakit_cpp_interface.a(interface.cpp.o): in function `__static_initialization_and_destruction_0(int, int)':
/usr/include/c++/9/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: /usr/include/c++/9/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: /home/dev/orwell/liborwell_rust/zlmediakit_rust/target/debug/build/zlmediakit_rust-499cc82f14515635/out/liblibzlmediakit_cpp_interface.a(interface.cpp.o):(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
I've researched and found some answers (Link error "undefined reference to `__gxx_personality_v0'" and g++) that old me to link against libstdc++. As you see, I'm already doing that.
If I try to compile the CMake project alone, it links sucessfully. However then I compile everything from the Rust side, it gives this error.
Complete output of rust compilation

CMake undefined reference with statically linked libraries

I am kinda new to CMake and trying to learn it. Im trying to compile an example project from an IDE with CMake. A problem arises when i try to link some of the vendor supplied static libraries.
The Project is basically:
PROJECT_DIR
source
main.c
include
main.h
device
familyfolder
morefolders
emlib
src
somesource.c
em_core.c
...
inc
someheader.h
em_core.h
...
protocol
bluetooth/lib
EFR32BG1P
libbluetooth.a
librail.a
libnvm3.a
...
bluetooth/ble_stack
src
host
gecko_bglib.c
soc
...
inc
soc
native_gecko.h
someheader1.h
someheader2.h
...
host
someheader3.h
common
bg_types.h (which is included by native_gecko.h)
someheader4.h
CMakeLists.txt within root of project
# Base EFM32 CMake file
#
# This can be used as is as a project base, or by adding the efm32-base
# repository as a submodule to another project, copying this CMakeLists file
# to the top level directory, and updating the BASE_LOCATION variable to reflect this
# change
#
## Copyright (c) 2016 Ryan Kurte
# This file is covered under the MIT license available at: https://opensource.org/licenses/MIT
###### Project Environment #####
# Set minimum CMake version
cmake_minimum_required(VERSION 2.8.4)
# Optional verbose mode, uncomment or pass in -DCMAKE_VERBOSE_MAKEFILE=ON
# set(CMAKE_VERBOSE_MAKEFILE ON)
set(BASE_LOCATION .)
# Set the compiler (must be prior to project setup)
include(${BASE_LOCATION}/toolchain/arm-gcc.cmake)
##### Project Setup #####
# Configure project and languages
project(efm32-test C CXX ASM)
# ${DEVICE} sets the target specific model name
if (NOT DEVICE)
set(DEVICE EFR32BG1P232F256GM48)
# set(DEVICE BGM13P22F512GA)
set(BOARD BRD4100A)
# set(BLE_LIB EFR32BG13P)
endif ()
# ${JLINK_DEVICE} device model setting specifically for JLink. (Defaults to ${DEVICE} when not set)
# set(JLINK_DEVICE EFM32TG11BXXXF128)
# When ${JLINK_SERVER_IP} is set, JLink will try to connect over IP to a JLink server
# set(JLINK_SERVER_IP 127.0.0.1)
# Set build
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE DEBUG)
endif ()
##### Modules #####
# Libraries can be added to the LIBS variable
# or manually included here.
# Add base libs (emlib, CMSIS, device files)
include(${BASE_LOCATION}/toolchain/efm32-base.cmake)
add_subdirectory(${BASE_LOCATION}/cmsis)
add_subdirectory(${BASE_LOCATION}/emlib)
add_subdirectory(${BASE_LOCATION}/emdrv)
add_subdirectory(${BASE_LOCATION}/device)
add_subdirectory(${BASE_LOCATION}/protocol)
add_subdirectory(${BASE_LOCATION}/hardware)
add_subdirectory(${BASE_LOCATION}/radio)
# This is normally set in efm32-base.cmake, but libraries may modify it so set
# it after libraries/subdirectories have been added
set(CMAKE_EXE_LINKER_FLAGS "${COMMON_DEFINITIONS} -Xlinker -T${LINKER_SCRIPT} -Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_PROJECT_NAME}.map -Wl,--gc-sections -Wl,-v")
##### Files #####
# Add project headers
include_directories(${BASE_LOCATION}/include)
# Generate executable and link
add_executable(${PROJECT_NAME}
source/main.c
source/init_mcu.c
source/init_board.c
source/init_app.c
source/gatt_db.c
source/pti.c
source/app.c
source/application_properties.c)
efm32_configure_linker_addresses(${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} ${LIBS} emlib emdrv cmsis device radio)
# Link optional libraries if available
if (${HAS_HARDWARE})
target_link_libraries(${PROJECT_NAME} hardware)
endif ()
if (${HAS_PROTOCOL})
target_link_libraries(${PROJECT_NAME} protocol)
endif ()
##### Post build #####
# Add post build commands
include(${BASE_LOCATION}/toolchain/post-build.cmake)
# Add JLink commands
include(${BASE_LOCATION}/toolchain/jlink.cmake)
##### CMake debug prints #####
if (CMAKE_VERBOSE_MAKEFILE)
print_debug_info()
endif()
CMakeLists.txt within emlib
# EFM32 Emlib CMake file
project(emlib)
# find . -name '*.c' | sort -u
add_library(${PROJECT_NAME}
src/em_core.c
src/alot_other_files.c)
target_include_directories(${PROJECT_NAME} PUBLIC inc)
target_link_libraries(${PROJECT_NAME} device protocol)
CMakeList.txt within protocol
project(protocol)
string(TOLOWER ${DEVICE} DEVICE_L)
set(BLE_LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/ble_stack/linker/GCC/${DEVICE_L}.ld")
if (NOT EXISTS ${BLE_LINKER_SCRIPT})
message("No bluetooth specific linker script defined, skipping protocol")
set(HAS_PROTOCOL FALSE PARENT_SCOPE)
return()
endif ()
set(HAS_PROTOCOL TRUE PARENT_SCOPE)
message("Found bluetooth specific linker script, using this instead:\n before: ${LINKER_SCRIPT}\n after: ${BLE_LINKER_SCRIPT}")
set(LINKER_SCRIPT ${BLE_LINKER_SCRIPT} PARENT_SCOPE)
add_library(${PROJECT_NAME}
bluetooth/ble_stack/src/host/gecko_bglib.c)
# bluetooth/ble_stack/src/soc/rtos_bluetooth.c)
target_include_directories(${PROJECT_NAME} PUBLIC
bluetooth/ble_stack/inc/soc
bluetooth/ble_stack/inc/host
bluetooth/ble_stack/inc/common)
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/libmbedtls.a
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/libcoex.a
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/libpsstore.a
emlib)
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a
emlib)
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/librail.a
emlib)
target_link_libraries(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a
emlib)
Start if main.c - the includes of which native_gecko.h is the main one.
/* Board headers */
#include "init_mcu.h"
#include "init_board.h"
#include "init_app.h"
#include "ble-configuration.h"
#include "board_features.h"
/* Bluetooth stack headers */
#include "bg_types.h"
#include "native_gecko.h"
#include "gatt_db.h"
/* Libraries containing default Gecko configuration values */
#include "em_emu.h"
#include "em_cmu.h"
When i try to include the static libraries and link them to the dependency of emlib
target_link_libraries(protocol PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/bluetooth/lib/EFR32BG1P/GCC/librail.a
emlib)
in a CMakeLists.txt file in the protocol folder, im getting a
undefined reference to some function
which should be in the libs folder and also is getting compiled beforehand.
As asked, more or less the full error part. which is at the moment mostly a repetition of static library dependency errors.
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(smp.c.obj): In function `smp_state_master_idle':
smp.c:(.text.smp_state_master_idle+0x298): undefined reference to `mbedtls_mpi_free'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(smp.c.obj): In function `smp_init':
smp.c:(.text.smp_init+0x4c): undefined reference to `mbedtls_ecp_group_init'
smp.c:(.text.smp_init+0x54): undefined reference to `mbedtls_ecp_group_load'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(sl_malloc.c.obj): In function `sl_malloc':
sl_malloc.c:(.text.sl_malloc+0x4): undefined reference to `CORE_EnterCritical'
sl_malloc.c:(.text.sl_malloc+0x14): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(sl_malloc.c.obj): In function `sl_free':
sl_malloc.c:(.text.sl_free+0x4): undefined reference to `CORE_EnterCritical'
sl_malloc.c:(.text.sl_free+0x16): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(vectors_efr32xg1x.c.obj): In function `vectors_register':
vectors_efr32xg1x.c:(.text.vectors_register+0x6): undefined reference to `CORE_SetNvicRamTableHandler'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(vectors_efr32xg1x.c.obj): In function `vectors_init':
vectors_efr32xg1x.c:(.text.vectors_init+0x1c): undefined reference to `CORE_InitNvicVectorTable'
vectors_efr32xg1x.c:(.text.vectors_init+0x24): undefined reference to `CORE_SetNvicRamTableHandler'
vectors_efr32xg1x.c:(.text.vectors_init+0x2c): undefined reference to `CORE_SetNvicRamTableHandler'
vectors_efr32xg1x.c:(.text.vectors_init+0x34): undefined reference to `CORE_SetNvicRamTableHandler'
vectors_efr32xg1x.c:(.text.vectors_init+0x3c): undefined reference to `CORE_SetNvicRamTableHandler'
vectors_efr32xg1x.c:(.text.vectors_init+0x44): undefined reference to `CORE_SetNvicRamTableHandler'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(vectors_efr32xg1x.c.obj):vectors_efr32xg1x.c:(.text.vectors_init+0x4c): more undefined references to `CORE_SetNvicRamTableHandler' follow
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(bg_random_aes.c.obj): In function `aes_ctr_encrypt':
bg_random_aes.c:(.text.aes_ctr_encrypt+0x26): undefined reference to `mbedtls_aes_setkey_enc'
bg_random_aes.c:(.text.aes_ctr_encrypt+0x3a): undefined reference to `mbedtls_aes_crypt_ctr'
protocol/bluetooth/lib/EFR32BG1P/GCC/libbluetooth.a(bg_util_aes.c.obj): In function `bg_init_aes_context':
bg_util_aes.c:(.text.bg_init_aes_context+0x2): undefined reference to `mbedtls_aes_init'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rail_rf.o): In function `RAILINT_e0dfaecc04939a24e643c7a83fed51fa':
rail_rf.c:(.text.RAILInt_SetChannel+0x6): undefined reference to `CORE_EnterCritical'
rail_rf.c:(.text.RAILInt_SetChannel+0x28): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rail_rf_hal.o): In function `RAILINT_2d49aecfcb6d27d54075c2234a55d5e7':
rail_rf_hal.c:(.text.RFHAL_ConfigRxOptions+0x8c): undefined reference to `CORE_EnterCritical'
rail_rf_hal.c:(.text.RFHAL_ConfigRxOptions+0xb0): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rail_rf_hal.o): In function `RAILINT_3212ddcec5ace9cb56e8f1cb3ca8d33c':
rail_rf_hal.c:(.text.RFHAL_SetTimer+0x1a): undefined reference to `CORE_EnterCritical'
rail_rf_hal.c:(.text.RFHAL_SetTimer+0x5a): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rail_rf_hal.o): In function `RAILINT_39475387e0cb29596ec425b4cfae76fb':
rail_rf_hal.c:(.text.RFHAL_GetRxPacketInfo+0x1a): undefined reference to `CORE_EnterCritical'
rail_rf_hal.c:(.text.RFHAL_GetRxPacketInfo+0x30): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rail_calibration.o): In function `RAILINT_3f0b6529856489702e1c986caf70e7aa':
rail_calibration.c:(.text.RAILInt_PendCal+0x6): undefined reference to `CORE_EnterCritical'
rail_calibration.c:(.text.RAILInt_PendCal+0x14): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_bufc.o): In function `RAILINT_8309d0303dd52c35776f1b9d204e124b':
rfhal_bufc.c:(.text.BUFC_SetTxBuffer+0xa): undefined reference to `CORE_EnterCritical'
rfhal_bufc.c:(.text.BUFC_SetTxBuffer+0x26): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_bufc.o): In function `RAILINT_ea701df21b15aeabbf4f1b9be517a6fb':
rfhal_bufc.c:(.text.BUFC_RxBufferReset+0x2): undefined reference to `CORE_EnterCritical'
rfhal_bufc.c:(.text.BUFC_RxBufferReset+0x64): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_bufc.o): In function `RAILINT_e5f98ee4df05ffeec0898fd3dfcac7ef':
rfhal_bufc.c:(.text.BUFC_ReleaseRxPacket+0xa): undefined reference to `CORE_EnterCritical'
rfhal_bufc.c:(.text.BUFC_ReleaseRxPacket+0x2c): undefined reference to `CORE_ExitCritical'
rfhal_bufc.c:(.text.BUFC_ReleaseRxPacket+0xda): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_bufc.o): In function `RAILINT_ee328b192a18bb4569d7a778a9299c02':
rfhal_bufc.c:(.text.BUFC_GetRxPacketInfo+0xa): undefined reference to `CORE_EnterCritical'
rfhal_bufc.c:(.text.BUFC_GetRxPacketInfo+0x4c): undefined reference to `CORE_ExitCritical'
rfhal_bufc.c:(.text.BUFC_GetRxPacketInfo+0xfa): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_protimer.o): In function `RAILINT_fea48c6a0a5dcb75b3cd7e0e0188d110':
rfhal_protimer.c:(.text.PROTIMER_CCTimerStart+0xa): undefined reference to `CORE_EnterCritical'
rfhal_protimer.c:(.text.PROTIMER_CCTimerStart+0x1c): undefined reference to `CORE_ExitCritical'
rfhal_protimer.c:(.text.PROTIMER_CCTimerStart+0x44): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_protimer.o): In function `RAILINT_6d18a45e267fa35e89a132429cb09fd9':
rfhal_protimer.c:(.text.PROTIMER_CheckCcaReallyFailed+0x4): undefined reference to `CORE_EnterAtomic'
rfhal_protimer.c:(.text.PROTIMER_CheckCcaReallyFailed+0xa2): undefined reference to `CORE_ExitAtomic'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_radio.o): In function `RAILINT_49c1638648869baaf18ca93f541e7dd3':
rfhal_radio.c:(.text.RADIO_GetRssi+0xc): undefined reference to `CORE_EnterCritical'
rfhal_radio.c:(.text.RADIO_GetRssi+0x36): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_rtccsync.o): In function `RAILINT_b3ae6c670ad30ab880888943a58faee0':
rfhal_rtccsync.c:(.text.RTCCSYNC_PreSleep+0x12): undefined reference to `CORE_EnterCritical'
rfhal_rtccsync.c:(.text.RTCCSYNC_PreSleep+0xda): undefined reference to `CORE_ExitCritical'
rfhal_rtccsync.c:(.text.RTCCSYNC_PreSleep+0x1cc): undefined reference to `CORE_ExitCritical'
rfhal_rtccsync.c:(.text.RTCCSYNC_PreSleep+0x21a): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_rtccsync.o): In function `RAILINT_b59eec27e1e4352a569b2404f6e35a1b':
rfhal_rtccsync.c:(.text.RTCCSYNC_PostWakeUp+0x24): undefined reference to `CORE_EnterCritical'
rfhal_rtccsync.c:(.text.RTCCSYNC_PostWakeUp+0xe6): undefined reference to `CORE_ExitCritical'
rfhal_rtccsync.c:(.text.RTCCSYNC_PostWakeUp+0x124): undefined reference to `CORE_EnterCritical'
rfhal_rtccsync.c:(.text.RTCCSYNC_PostWakeUp+0x166): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(rfhal_rand.o): In function `RAILINT_06d3d408af442bc7f395514867b33be3':
rfhal_rand.c:(.text.RFRAND_GetRadioEntropy+0x36): undefined reference to `CORE_EnterCritical'
rfhal_rand.c:(.text.RFRAND_GetRadioEntropy+0xa0): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_5f7114cf3ce03d53615d89fe78177694':
generic_phy.c:(.text.GENERIC_PHY_ConfigureRollbackReporting+0x4): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_ConfigureRollbackReporting+0x46): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_933bfa2aa7cb94f384fd9482dc3d4942':
generic_phy.c:(.text.RADIO_RACRxAbort+0x2): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.RADIO_RACRxAbort+0x2c): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_661cd5b832e0dc684eca55cf65a1a4bd':
generic_phy.c:(.text.GENERIC_PHY_DisableRadioIrqs+0x2): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_DisableRadioIrqs+0x90): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_549ae7c6edbff50f2340752aad688b72':
generic_phy.c:(.text.GENERIC_PHY_ConfigureEvents+0x2e): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_ConfigureEvents+0x60): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_ConfigureEvents+0xa2): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_ConfigureEvents+0x2ec): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_ConfigureEvents+0x5c): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_cc007e4287f0aeed09681d7e13e3e8eb':
generic_phy.c:(.text.GENERIC_PHY_PacketTx+0x2): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_PacketTx+0x1c): undefined reference to `CORE_ExitCritical'
generic_phy.c:(.text.GENERIC_PHY_PacketTx+0x30): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_0398a16a4bf50dbcafa68c979c0efcc9':
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketRx+0x14): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketRx+0x2a): undefined reference to `CORE_ExitCritical'
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketRx+0x5e): undefined reference to `CORE_ExitCritical'
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketRx+0xd0): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_c508b1e616a62d61aefaf693649607f1':
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketTx+0x4): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketTx+0x1e): undefined reference to `CORE_ExitCritical'
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketTx+0x5a): undefined reference to `CORE_ExitCritical'
generic_phy.c:(.text.GENERIC_PHY_SchedulePacketTx+0x64): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_26621d98bc1a2dc74b7093edea89d850':
generic_phy.c:(.text.GENERIC_PHY_ResetRxFifo+0x4): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_ResetRxFifo+0x1a): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_232b67d119ad2a1a23589ef6ecfa72b8':
generic_phy.c:(.text.GENERIC_PHY_RadioIdle+0x8): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_RadioIdle+0x26): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `FRC_IRQHandler':
generic_phy.c:(.text.FRC_IRQHandler+0x436): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.FRC_IRQHandler+0x466): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/librail.a(generic_phy.o): In function `RAILINT_1446b673acaddfc4b02974cda9fee981':
generic_phy.c:(.text.GENERIC_PHY_SetFeatures+0x6): undefined reference to `CORE_EnterCritical'
generic_phy.c:(.text.GENERIC_PHY_SetFeatures+0x16): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a(nvm3_lock.c.obj): In function `nvm3_lockBegin':
nvm3_lock.c:(.text.nvm3_lockBegin+0x2): undefined reference to `CORE_EnterCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a(nvm3_lock.c.obj): In function `nvm3_lockEnd':
nvm3_lock.c:(.text.nvm3_lockEnd+0x4): undefined reference to `CORE_ExitCritical'
protocol/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a(nvm3_hal_flash.c.obj): In function `nvm3_halFlashWriteWords':
nvm3_hal_flash.c:(.text.nvm3_halFlashWriteWords+0xa): undefined reference to `MSC_WriteWord'
protocol/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a(nvm3_hal_flash.c.obj): In function `nvm3_halFlashClose':
nvm3_hal_flash.c:(.text.nvm3_halFlashClose+0x0): undefined reference to `MSC_Deinit'
protocol/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a(nvm3_hal_flash.c.obj): In function `nvm3_halFlashOpen':
nvm3_hal_flash.c:(.text.nvm3_halFlashOpen+0x2): undefined reference to `MSC_Init'
protocol/bluetooth/lib/EFR32BG1P/GCC/libnvm3.a(nvm3_hal_flash.c.obj): In function `nvm3_halFlashPageErase':
nvm3_hal_flash.c:(.text.nvm3_halFlashPageErase+0x4): undefined reference to `MSC_ErasePage'
collect2: error: ld returned 1 exit status
CMakeFiles/efm32-test.dir/build.make:290: recipe for target 'efm32-test' failed
make[2]: *** [efm32-test] Error 1
CMakeFiles/Makefile2:261: recipe for target 'CMakeFiles/efm32-test.dir/all' failed
make[1]: *** [CMakeFiles/efm32-test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

How do I use SFML with CMake FetchContent?

So I'd like to get the SFML from the git tag directly using CMake FetchContent. Most of the tutorial are not using this, so I don't really know what to do, I use imgui-sfml-fetchcontent for the reference.
My CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
include(FetchContent)
project(2DComputerGraphics)
set(CMAKE_CXX_STANDARD 17)
# SFML
set(SFML_VERSION "2.5.1")
FetchContent_Declare(
SFML
GIT_REPOSITORY "https://github.com/SFML/SFML.git"
GIT_TAG "${SFML_VERSION}"
)
FetchContent_GetProperties(SFML)
if(NOT SFML_POPULATED)
FetchContent_Populate(SFML)
add_subdirectory(${SFML_SOURCE_DIR} ${SFML_BINARY_DIR})
#
# message("SFML_SOURCE_DIR: ${SFML_SOURCE_DIR}")
# message("SFML_BINARY_DIR: ${SFML_BINARY_DIR}")
endif()
set(SOURCE
"main.cpp"
)
#
# message("Source: ${SOURCE}")
add_executable(2DComputerGraphicsApp
"${SOURCE}"
)
target_include_directories(2DComputerGraphicsApp PRIVATE
"${SFML_INCLUDE_DIR}"
)
#
# message("SFML_INCLUDE_DIR: ${SFML_INCLUDE_DIR}")
target_link_libraries(2DComputerGraphicsApp
"${SFML_LIBRARIES}"
"${SFML_DEPENDENCIES}"
)
#
# message("SFML_LIBRARIES: ${SFML_LIBRARIES}")
# message("SFML_DEPENDENCIES: ${SFML_DEPENDENCIES}")
target_compile_options(2DComputerGraphicsApp PRIVATE -Wall)
And it gives me this error
/usr/bin/ld: CMakeFiles/2DComputerGraphicsApp.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x82): undefined reference to `sf::String::String(char const*, std::locale const&)'
/usr/bin/ld: main.cpp:(.text+0xa0): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
/usr/bin/ld: main.cpp:(.text+0xd3): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
/usr/bin/ld: main.cpp:(.text+0x100): undefined reference to `sf::Texture::Texture()'
/usr/bin/ld: main.cpp:(.text+0x119): undefined reference to `sf::Texture::create(unsigned int, unsigned int)'
/usr/bin/ld: main.cpp:(.text+0x132): undefined reference to `sf::Sprite::Sprite(sf::Texture const&)'
/usr/bin/ld: main.cpp:(.text+0x152): undefined reference to `sf::Window::isOpen() const'
/usr/bin/ld: main.cpp:(.text+0x173): undefined reference to `sf::Window::pollEvent(sf::Event&)'
/usr/bin/ld: main.cpp:(.text+0x190): undefined reference to `sf::Window::close()'
/usr/bin/ld: main.cpp:(.text+0x1da): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
/usr/bin/ld: main.cpp:(.text+0x1f7): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
/usr/bin/ld: main.cpp:(.text+0x210): undefined reference to `sf::Texture::update(unsigned char const*)'
/usr/bin/ld: main.cpp:(.text+0x229): undefined reference to `sf::RenderStates::Default'
/usr/bin/ld: main.cpp:(.text+0x234): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
/usr/bin/ld: main.cpp:(.text+0x243): undefined reference to `sf::Window::display()'
/usr/bin/ld: main.cpp:(.text+0x284): undefined reference to `sf::Texture::~Texture()'
/usr/bin/ld: main.cpp:(.text+0x293): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: main.cpp:(.text+0x2fd): undefined reference to `sf::Texture::~Texture()'
/usr/bin/ld: main.cpp:(.text+0x311): undefined reference to `sf::RenderWindow::~RenderWindow()'
/usr/bin/ld: CMakeFiles/2DComputerGraphicsApp.dir/main.cpp.o: in function `sf::Sprite::~Sprite()':
main.cpp:(.text._ZN2sf6SpriteD2Ev[_ZN2sf6SpriteD5Ev]+0xf): undefined reference to `vtable for sf::Sprite'
/usr/bin/ld: main.cpp:(.text._ZN2sf6SpriteD2Ev[_ZN2sf6SpriteD5Ev]+0x1d): undefined reference to `vtable for sf::Sprite'
/usr/bin/ld: main.cpp:(.text._ZN2sf6SpriteD2Ev[_ZN2sf6SpriteD5Ev]+0x35): undefined reference to `sf::Transformable::~Transformable()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/2DComputerGraphicsApp.dir/build.make:84: 2DComputerGraphicsApp] Error 1
make[3]: Leaving directory '/home/andraantariksa/Projects/2d-computer-graphics/build'
make[2]: *** [CMakeFiles/Makefile2:216: CMakeFiles/2DComputerGraphicsApp.dir/all] Error 2
make[2]: Leaving directory '/home/andraantariksa/Projects/2d-computer-graphics/build'
make[1]: *** [Makefile:130: all] Error 2
make[1]: Leaving directory '/home/andraantariksa/Projects/2d-computer-graphics/build'
make: *** [Makefile:2: build] Error 2
I think it happen because I did not link the SFML correctly. How do I solve this?
The variables are wrong. https://cmake.org/cmake/help/v3.16/module/FetchContent.html states that <name> has to be lower case. Also squareskittles is right, you shouldn't use ${SFML_LIBRARIES} / ${SFML_INCLUDE_DIR}, this is only valid if the old FindSFML.cmake is used.
cmake_minimum_required(VERSION 3.10)
include(FetchContent)
project(2DComputerGraphics)
set(CMAKE_CXX_STANDARD 17)
set(SFML_VERSION "2.5.1")
FetchContent_Declare(
sfml
GIT_REPOSITORY "https://github.com/SFML/SFML.git"
GIT_TAG "${SFML_VERSION}"
)
FetchContent_GetProperties(sfml)
if(NOT sfml_POPULATED)
FetchContent_Populate(sfml)
add_subdirectory(${sfml_SOURCE_DIR} ${sfml_BINARY_DIR})
endif()
set(SOURCE
main.cpp
)
add_executable(2DComputerGraphicsApp
${SOURCE}
)
target_link_libraries(2DComputerGraphicsApp
PRIVATE
sfml-audio
sfml-graphics
sfml-system
sfml-window
)
target_compile_options(2DComputerGraphicsApp PRIVATE -Wall)