I try to learn how to use cmake, and so I created a little project but when I try to compile I get this error: /usr/bin/ld : CMakeFiles/test.dir/main.cpp.o : dans la fonction « main » : main.cpp:(.text+0x2d) : référence indéfinie vers « la::Matrice<int>::Matrice(unsigned int, unsigned int) »
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── LinearAlgebra
│ └── Makefile
├── CMakeLists.txt
├── LinearAlgebra
│ ├── CMakeLists.txt
│ ├── Matrice.cpp
│ └── Matrice.hpp
└── main.cpp
./CmakeLists.txt:
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true)
project(test_project)
add_subdirectory(LinearAlgebra)
add_executable(test main.cpp)
link_libraries(test linear_algebra)
LinearAlgebra/CMakeLists.txt:
cmake_minimum_required(VERSION 3.10)
project(linear_algebra)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(SOURCES
Matrice.cpp
)
set(HEADERS
Matrice.hpp
)
add_library(linear_algebra ${HEADERS} ${SOURCES})
Do someone know where the problem is?
Withlink_libraries
you define which libraries will be linked for targets that are defined after this command and within the current directory.
You probably meant to use target_link_libraries
which defines libraries to link to the given target. The documentation suggests to use this over link_libraries
whenever possible. It is explicit and better scoped.
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED true)
project(test_project)
add_subdirectory(LinearAlgebra)
add_executable(test main.cpp)
target_link_libraries(test linear_algebra)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.