Link Windows CMake 项目中的 SFML 库错误
Link error with SFML library in CMake project on Windows
我正在尝试在 Windows/MinGW 和 link 上构建一个 CMake 项目到 SFML 2.5.1。 CMake 似乎找到了库,程序编译正常,但我收到 'undefined reference' linker 错误。我跟着SFML 2.5 CMake build instructions。我错过了什么?
错误:
[ 50%] Linking CXX executable pressure.exe
CMakeFiles\pressure.dir/objects.a(main.cpp.obj): In function `main':
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf6StringC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6locale'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:/code/cpp/small/pressure/main.cpp:21: undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
C:/code/cpp/small/pressure/main.cpp:24: undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:/code/cpp/small/pressure/main.cpp:26: undefined reference to `_imp___ZN2sf6Window5closeEv'
C:/code/cpp/small/pressure/main.cpp:30: undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
C:/code/cpp/small/pressure/main.cpp:30: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:/code/cpp/small/pressure/main.cpp:32: undefined reference to `_imp___ZN2sf6Window7displayEv'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
collect2.exe: error: ld returned 1 exit status
我的项目CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(pressure)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
if(WIN32)
#set(SFML_STATIC_LIBRARIES TRUE)
set(SFML_DIR C:/lib/cpp/SFML-2.5.1/lib/cmake/SFML)
endif()
set(SFML_LIBRARIES sfml-graphics sfml-audio)
set(pressure_VERSION_MAJOR 0)
set(pressure_VERSION_MINOR 1)
configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
include_directories("${PROJECT_BINARY_DIR}")
set(EXECUTABLE_NAME "pressure")
find_package(SFML 2.5 COMPONENTS system window graphics audio main network REQUIRED)
add_executable(pressure main.cpp)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
此错误已在 Stack Overflow 网站上多次提及:
- Cannot compile SFML project using cmake
- CMake+SFML Linker error, even though library is specified in CMakeLists.txt
这种特定 链接器错误通常发生在 SFML 库与其本身 不兼容 时 链接到。 SFML 2.5.1 下载页面提供了多个版本的 SFML 预构建库。确保下载并引用与 您的 编译器匹配的版本(例如 MinGW、Visual C++ 12、Visual C++ 15 等)。
在Windows上,支持多种不同的编译器;因此,例如,如果您使用 Visual Studio 15 构建项目,请确保您已经下载并引用了 Visual C++ 15 SFML 库。还要确保 SFML 库与您的编译器(即 32 位或 64 位)的 体系结构 相匹配。
如果您在 SFML 下载页面提供的列表中没有看到您正在使用的编译器,您可以下载 SFML 源代码并使用您的编译器自行构建 .这将有助于确保 SFML 库与您的项目具有二进制兼容性。或者,您可以浏览 older versions SFML 预构建库,看看是否有任何库与您在项目中使用的编译器相匹配。
我正在尝试在 Windows/MinGW 和 link 上构建一个 CMake 项目到 SFML 2.5.1。 CMake 似乎找到了库,程序编译正常,但我收到 'undefined reference' linker 错误。我跟着SFML 2.5 CMake build instructions。我错过了什么?
错误:
[ 50%] Linking CXX executable pressure.exe
CMakeFiles\pressure.dir/objects.a(main.cpp.obj): In function `main':
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf6StringC1ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6locale'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:/code/cpp/small/pressure/main.cpp:21: undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
C:/code/cpp/small/pressure/main.cpp:24: undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:/code/cpp/small/pressure/main.cpp:26: undefined reference to `_imp___ZN2sf6Window5closeEv'
C:/code/cpp/small/pressure/main.cpp:30: undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
C:/code/cpp/small/pressure/main.cpp:30: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:/code/cpp/small/pressure/main.cpp:32: undefined reference to `_imp___ZN2sf6Window7displayEv'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:/code/cpp/small/pressure/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
collect2.exe: error: ld returned 1 exit status
我的项目CMakeLists.txt:
cmake_minimum_required(VERSION 3.16)
project(pressure)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
if(WIN32)
#set(SFML_STATIC_LIBRARIES TRUE)
set(SFML_DIR C:/lib/cpp/SFML-2.5.1/lib/cmake/SFML)
endif()
set(SFML_LIBRARIES sfml-graphics sfml-audio)
set(pressure_VERSION_MAJOR 0)
set(pressure_VERSION_MINOR 1)
configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
)
include_directories("${PROJECT_BINARY_DIR}")
set(EXECUTABLE_NAME "pressure")
find_package(SFML 2.5 COMPONENTS system window graphics audio main network REQUIRED)
add_executable(pressure main.cpp)
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
此错误已在 Stack Overflow 网站上多次提及:
- Cannot compile SFML project using cmake
- CMake+SFML Linker error, even though library is specified in CMakeLists.txt
这种特定 链接器错误通常发生在 SFML 库与其本身 不兼容 时 链接到。 SFML 2.5.1 下载页面提供了多个版本的 SFML 预构建库。确保下载并引用与 您的 编译器匹配的版本(例如 MinGW、Visual C++ 12、Visual C++ 15 等)。
在Windows上,支持多种不同的编译器;因此,例如,如果您使用 Visual Studio 15 构建项目,请确保您已经下载并引用了 Visual C++ 15 SFML 库。还要确保 SFML 库与您的编译器(即 32 位或 64 位)的 体系结构 相匹配。
如果您在 SFML 下载页面提供的列表中没有看到您正在使用的编译器,您可以下载 SFML 源代码并使用您的编译器自行构建 .这将有助于确保 SFML 库与您的项目具有二进制兼容性。或者,您可以浏览 older versions SFML 预构建库,看看是否有任何库与您在项目中使用的编译器相匹配。