【问题标题】:CMake linker not working correctlyCMake 链接器无法正常工作
【发布时间】:2014-10-14 18:31:12
【问题描述】:

我正在尝试使用 CMake 构建我的项目,但在链接所需库时出错。我在项目的根文件夹中有这个 CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project(test)
add_subdirectory(src)

在我的 src 文件夹中,在我的源文件旁边,我有这个 CMakeLists.txt:

set (CMAKE_CXX_FLAGS "Wall -std=c++11" )
set (CMAKE_EXE_LINKER_FLAGS "-lSDL2 -lGL" )
file (GLOB SRCS *.cpp *.h )
add_executable(engine ${SRCS} )

然后我进入构建文件夹并执行cmake ..,它运行时没有任何错误。当我做make 时,编译运行也没有任何错误,但是当它到达链接部分时,我得到了这些错误:

CMakeFiles/test.dir/Application.cpp.o: In function `Application::onExecute()':
Application.cpp:(.text+0x41): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0x4e): undefined reference to `SDL_GetTicks'
Application.cpp:(.text+0xd7): undefined reference to `SDL_PollEvent'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::render()':
Application.cpp:(.text+0x17b): undefined reference to `glClearColor'
Application.cpp:(.text+0x185): undefined reference to `glClear'
Application.cpp:(.text+0x194): undefined reference to `SDL_GL_SwapWindow'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::cleanUp()':
Application.cpp:(.text+0x1b2): undefined reference to `SDL_GL_DeleteContext'
Application.cpp:(.text+0x1c1): undefined reference to `SDL_DestroyWindow'
Application.cpp:(.text+0x1c6): undefined reference to `SDL_Quit'
CMakeFiles/test.dir/Application.cpp.o: In function `Application::initialize()':
Application.cpp:(.text+0x1de): undefined reference to `SDL_Init'
Application.cpp:(.text+0x1ea): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x22b): undefined reference to `SDL_CreateWindow'
Application.cpp:(.text+0x243): undefined reference to `SDL_GetError'
Application.cpp:(.text+0x25a): undefined reference to `SDL_Quit'
Application.cpp:(.text+0x270): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x27f): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x28e): undefined reference to `SDL_GL_SetAttribute'
Application.cpp:(.text+0x29d): undefined reference to `SDL_GL_CreateContext'
Application.cpp:(.text+0x2af): undefined reference to `glGetString'
collect2: error: ld returned 1 exit status
make[2]: *** [src/test] Error 1
make[1]: *** [src/CMakeFiles/test.dir/all] Error 2
make: *** [all] Error 2

我的头文件中有正确的包含,并且我能够仅使用 make 编译和运行,所以我认为我告诉 CMake 的链接器标志没有被传递给编译器。我该如何解决这个问题?

【问题讨论】:

  • 当你使用“only make”编译时,这是否意味着你编写了自己的makefile?该 makefile 中有什么不在 CMake 生成的文件中?
  • 是的,我已经编写了自己的 makefile。而且我在 CMake 生成的 makefile 中的任何地方都看不到链接器选项。虽然我也看不到编译器标志,但它们似乎可以工作

标签: c++ linker cmake


【解决方案1】:

您必须使用 cmake 命令 target_link_libraries 来引用 SDL 库。

【讨论】:

    猜你喜欢
    • 2012-12-27
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2017-04-30
    • 2017-11-28
    • 2014-12-15
    相关资源
    最近更新 更多