我基本上是想在CLion中创建一个使用CMake的SFML项目。
我首先下载了已经构建了.dylib
个文件的SFML 2.2。它说要在usr/local/lib
和usr/local/include
中安装它们,但它们不存在,所以我创建它们然后将文件放在那里。
然后我打开了CLion,创建了一个子目录cmake_modules
,将FindSFML.cmake
文件放在里面,并在根项目目录中放置了这些文件:
的main.cpp
#include <SFML/Graphics.hpp>
int main()
{
sf::err() << "Hello, World!" << std::endl;
return 0;
}
的CMakeLists.txt :
cmake_minimum_required(VERSION 3.2)
project(HelloWorld)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(HelloWorld ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML COMPONENTS graphics window system REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(HelloWorld ${SFML_Libraries})
问题在于,当我尝试构建项目时,出现以下错误:
Undefined symbols for architecture x86_64:
"sf::err()", 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)
我尝试过寻找解决方案,但似乎没有一个能够解决问题。我把文件放在错误的位置,还是忘记了一些导入CMake设置?我知道用Xcode设置SFML会更容易,但我更愿意尽可能使用CLion。
答案 0 :(得分:2)
Ok, so I made an entirely new project, ran ->whereRaw('requests.updated_at > $last_check')
on my terminal to make xcode-select --install
a directory that gets searched by the compiler, downloaded SFML 2.3 instead of 2.2, decided to use the /usr/local/
file shipped with SFML, and made my FindSFML.cmake
file look like so:
CmakeLists.txt
This compiled and ran the following code in CLion:
cmake_minimum_required(VERSION 3.2)
project(SFMLTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(SFMLTest ${SOURCE_FILES})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "~/SFML-2.3/cmake/Modules/")
find_package(SFML REQUIRED graphics window system)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(SFMLTest ${SFML_LIBRARIES})
endif(SFML_FOUND)
I don't know what exactly fixed everything, but I'm just glad it did. Hopefully this helps anyone else who's in a similar jam.
答案 1 :(得分:2)
SFML 2.5 及更高版本采用了更现代的方法,设置 cmake 更容易。
示例 CMakeLists.txt
cmake_minimum_required(VERSION 3.1)
project(SFMLTest)
find_package(SFML 2.5 COMPONENTS graphics audio REQUIRED)
add_executable(SFMLTest main.cpp)
target_link_libraries(SFMLTest sfml-graphics sfml-audio)
注意:
<块引用>不再需要设置 FindSFML.cmake 或 SFML_ROOT 等
SFML_LIBRARIES、SFML_DEPENDENCIES 和 SFML_INCLUDE_DIR 不存在 没有了
答案 2 :(得分:0)
对于通过brew下载SFML的其他人,我设法通过将此添加到我的CMakeLists.txt来解决此问题
set(SFML_ROOT /usr/local/lib)