'How to link shared library on linux using cmake?
How to link shared library on linux platform? I downloaded sfml library using apt cmd and I tried to run simple example:
main.cpp
#include <SFML/Graphics.hpp>
int main()
{
// Make a window that is 800 by 200 pixels
// And has the title "Hello from SFML"
sf::RenderWindow window(sf::VideoMode(800, 200), "Hello from SFML");
return 0;
}
But I keep getting undefined reference even though vs code sees files and lets me jump directly to them using ctrl button.
cmake:
cmake_minimum_required(VERSION 3.0.0)
project(sflmProject VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(sflmProject main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
compilation error:
main] Building folder: firstSFLMProject
[build] Starting build
[main] Changes were detected in CMakeLists.txt but we could not reconfigure the project because another operation is already in progress.
[proc] Executing command: /usr/bin/cmake --build /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/build --config Debug --target all -j 10 --
[build] -- Configuring done
[build] -- Generating done
[build] -- Build files have been written to: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/build
[build] Consolidate compiler generated dependencies of target sflmProject
[build] [ 50%] Building CXX object CMakeFiles/sflmProject.dir/main.cpp.o
[build] [100%] Linking CXX executable sflmProject
[build] /usr/bin/ld: CMakeFiles/sflmProject.dir/main.cpp.o: in function `main':
[build] /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:16: undefined reference to `sf::String::String(char const*, std::locale const&)'
[build] /usr/bin/ld: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:16: undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
[build] /usr/bin/ld: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:16: undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
[build] /usr/bin/ld: /home/black_wraith/Documents/AIR/S8/RealTime/firstSFLMProject/main.cpp:63: undefined reference to `sf::RenderWindow::~RenderWindow()'
[build] collect2: error: ld returned 1 exit status
[build] gmake[2]: *** [CMakeFiles/sflmProject.dir/build.make:97: sflmProject] Error 1
[build] gmake[1]: *** [CMakeFiles/Makefile2:839: CMakeFiles/sflmProject.dir/all] Error 2
[build] gmake: *** [Makefile:121: all] Error 2
[build] Build finished with exit code 2
I tried to modify cmake like this but I just have no idea which file should I add:
cmake_minimum_required(VERSION 3.0.0)
project(sflmProject VERSION 0.1.0)
include(CTest)
enable_testing()
add_library(sfml SHARED /usr/include/SFML/*)
add_executable(sflmProject main.cpp)
target_link_libraries(sflmProject PRIVATE sfml)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Solution 1:[1]
here is an example of cmake file for sfml on linux
cmake_minimum_required(VERSION 3.22)
project(sfml-program LANGUAGES CXX)
add_executable(${PROJECT_NAME} main.cpp)
find_package (SFML 2.5 COMPONENTS graphics audio network REQUIRED)
target_link_libraries (${PROJECT_NAME} PUBLIC sfml-system sfml-graphics sfml-audio sfml-network)
Solution 2:[2]
Alternatively this works as well:
target_link_libraries(${PROJECT_NAME}
/usr/lib/x86_64-linux-gnu/libsfml-window.so
/usr/lib/x86_64-linux-gnu/libsfml-graphics.so
/usr/lib/x86_64-linux-gnu/libsfml-audio.so
/usr/lib/x86_64-linux-gnu/libsfml-network.so
/usr/lib/x86_64-linux-gnu/libsfml-system.so
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Gameriker |