- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试在 Windows 上使用 CMake 来构建一个包含 2 个库(GLEW 和 SDL2)的 C++ 项目,但是在这样做时我收到一个 undefined reference 错误,如下所示:-
PS D:\Projects\C++Projects\game_engine\build>> cmg ..
-- The C compiler identification is GNU 7.1.0
-- The CXX compiler identification is GNU 7.1.0
-- Check for working C compiler: C:/Program Files/MinGW-w64/mingw64/bin/gcc.exe
-- Check for working C compiler: C:/Program Files/MinGW-w64/mingw64/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files/MinGW-w64/mingw64/bin/g++.exe
-- Check for working CXX compiler: C:/Program Files/MinGW-w64/mingw64/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: D:/Projects/C++Projects/game_engine/build
Scanning dependencies of target game_engine
[ 25%] Building CXX object CMakeFiles/game_engine.dir/src/MainGame.cpp.obj
[ 50%] Building CXX object CMakeFiles/game_engine.dir/src/Sprite.cpp.obj
[ 75%] Building CXX object CMakeFiles/game_engine.dir/src/main.cpp.obj
[100%] Linking CXX executable game_engine.exe
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x4b): undefined reference to `SDL_Quit'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x141): undefined reference to `SDL_Init'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x175): undefined reference to `SDL_CreateWindow'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x1e4): undefined reference to `SDL_GL_CreateContext'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x240): undefined reference to `__imp_glewInit'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x2a4): undefined reference to `SDL_GL_SetAttribute'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x2c1): undefined reference to `glClearColor'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x39e): undefined reference to `SDL_PollEvent'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x438): undefined reference to `glClearDepth'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x442): undefined reference to `glClear'
CMakeFiles\game_engine.dir/objects.a(MainGame.cpp.obj):MainGame.cpp:(.text+0x461): undefined reference to `SDL_GL_SwapWindow'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x30): undefined reference to `__imp___glewDeleteBuffers'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0xae): undefined reference to `__imp___glewGenBuffers'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x15d): undefined reference to `__imp___glewBindBuffer'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x175): undefined reference to `__imp___glewBufferData'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x198): undefined reference to `__imp___glewBindBuffer'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x1c1): undefined reference to `__imp___glewBindBuffer'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x1d9): undefined reference to `__imp___glewEnableVertexAttribArray' CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x1ea): undefined reference to `__imp___glewVertexAttribPointer'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x22b): undefined reference to `glDrawArrays'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x232): undefined reference to `__imp___glewDisableVertexAttribArray'
CMakeFiles\game_engine.dir/objects.a(Sprite.cpp.obj):Sprite.cpp:(.text+0x243): undefined reference to `__imp___glewBindBuffer'
C:/PROGRA~1/MINGW-~1/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/7.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e): undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeFiles\game_engine.dir\build.make:118: game_engine.exe] Error 1
make[1]: *** [CMakeFiles\Makefile2:67: CMakeFiles/game_engine.dir/all] Error 2
make: *** [Makefile:83: all] Error 2
项目树是:-
Root
|-build
|-include
|-GL
|-SDL2
|-MainGame.h
\-Sprite.h
|-lib
|-glew32.lib
|-glew32s.lib
|-SDL2.lib
|-SDL2main.lib
\-SDL2test.lib
\-src
|-main.cpp
|-MainGame.cpp
\-Sprite.cpp
我的 CMakeLists.txt 文件如下所示:-
cmake_minimum_required(VERSION 3.11.4)
project (game_engine)
# Bring header files into scope
include_directories(include)
include_directories(include/SDL2)
include_directories(include/GL)
# Retrieve all source files
file(GLOB SOURCES "src/*.cpp")
add_executable(game_engine ${SOURCES})
target_link_libraries(game_engine -L./lib)
目前,我认为 undefined reference 是编译器未在 SDL 和 GLEW 库中找到相应的 .cpp 文件的结果。
这让我相信 CMakeLists.txt 文件中的最后一行没有成功链接两个库的 .lib 文件。
任何帮助都将不胜感激,如果上述错误是由于项目结构不佳造成的(我怀疑这是这里的主要问题),我将很乐意对其进行更改。
谢谢。
编辑(1):
我想添加一些我刚刚注意到的内容,即 cmake 实际上生成了一个 makefile,但紧接着运行 make 是产生 undefined reference 错误的原因。检查 makefile,我发现根本没有链接库,这再次证实 target_link_libraries() 无法链接 SDL 和 GLEW。
这是更新后的 CMakeLists.txt 文件:
cmake_minimum_required(VERSION 3.11.4)
# Project Name
project (game_engine)
# Retrieve all source files
set(SOURCES
${CMAKE_SOURCE_DIR}/src/main.cpp
${CMAKE_SOURCE_DIR}/src/MainGame.cpp
${CMAKE_SOURCE_DIR}/src/Sprite.cpp)
add_executable(game_engine ${SOURCES})
target_include_directories(game_engine PUBLIC
include
include/SDL2
include/GL)
target_link_libraries(game_engine
${CMAKE_SOURCE_DIR}/lib/glew32.lib
${CMAKE_SOURCE_DIR}/lib/glew32s.lib
${CMAKE_SOURCE_DIR}/lib/SDL2.lib
${CMAKE_SOURCE_DIR}/lib/SDL2main.lib
${CMAKE_SOURCE_DIR}/lib/SDL2test.lib)
和生成的 makefile:
# CMAKE generated file: DO NOT EDIT!
# Generated by "MinGW Makefiles" Generator, CMake Version 3.11
# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target
# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:
#=============================================================================
# Special targets provided by cmake.
# Disable implicit rules so canonical targets will work.
.SUFFIXES:
# Remove some rules from gmake that .SUFFIXES does not remove.
SUFFIXES =
.SUFFIXES: .hpux_make_needs_suffix_list
# Suppress display of executed commands.
$(VERBOSE).SILENT:
# A target that is always out of date.
cmake_force:
.PHONY : cmake_force
#=============================================================================
# Set environment variables for the build.
SHELL = cmd.exe
# The CMake executable.
CMAKE_COMMAND = D:\CMake\bin\cmake.exe
# The command to remove a file.
RM = D:\CMake\bin\cmake.exe -E remove -f
# Escaping for special characters.
EQUALS = =
# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = D:\Projects\C++Projects\game_engine
# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = D:\Projects\C++Projects\game_engine\build
#=============================================================================
# Targets provided globally by CMake.
# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake cache editor..."
D:\CMake\bin\cmake-gui.exe -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : edit_cache
# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast
# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..."
D:\CMake\bin\cmake.exe -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache
# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast
# The main all target
all: cmake_check_build_system
$(CMAKE_COMMAND) -E cmake_progress_start D:\Projects\C++Projects\game_engine\build\CMakeFiles D:\Projects\C++Projects\game_engine\build\CMakeFiles\progress.marks
$(MAKE) -f CMakeFiles\Makefile2 all
$(CMAKE_COMMAND) -E cmake_progress_start D:\Projects\C++Projects\game_engine\build\CMakeFiles 0
.PHONY : all
# The main clean target
clean:
$(MAKE) -f CMakeFiles\Makefile2 clean
.PHONY : clean
# The main clean target
clean/fast: clean
.PHONY : clean/fast
# Prepare targets for installation.
preinstall: all
$(MAKE) -f CMakeFiles\Makefile2 preinstall
.PHONY : preinstall
# Prepare targets for installation.
preinstall/fast:
$(MAKE) -f CMakeFiles\Makefile2 preinstall
.PHONY : preinstall/fast
# clear depends
depend:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles\Makefile.cmake 1
.PHONY : depend
#=============================================================================
# Target rules for targets named game_engine
# Build rule for target.
game_engine: cmake_check_build_system
$(MAKE) -f CMakeFiles\Makefile2 game_engine
.PHONY : game_engine
# fast build rule for target.
game_engine/fast:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/build
.PHONY : game_engine/fast
src/MainGame.obj: src/MainGame.cpp.obj
.PHONY : src/MainGame.obj
# target to build an object file
src/MainGame.cpp.obj:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/MainGame.cpp.obj
.PHONY : src/MainGame.cpp.obj
src/MainGame.i: src/MainGame.cpp.i
.PHONY : src/MainGame.i
# target to preprocess a source file
src/MainGame.cpp.i:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/MainGame.cpp.i
.PHONY : src/MainGame.cpp.i
src/MainGame.s: src/MainGame.cpp.s
.PHONY : src/MainGame.s
# target to generate assembly for a file
src/MainGame.cpp.s:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/MainGame.cpp.s
.PHONY : src/MainGame.cpp.s
src/Sprite.obj: src/Sprite.cpp.obj
.PHONY : src/Sprite.obj
# target to build an object file
src/Sprite.cpp.obj:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/Sprite.cpp.obj
.PHONY : src/Sprite.cpp.obj
src/Sprite.i: src/Sprite.cpp.i
.PHONY : src/Sprite.i
# target to preprocess a source file
src/Sprite.cpp.i:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/Sprite.cpp.i
.PHONY : src/Sprite.cpp.i
src/Sprite.s: src/Sprite.cpp.s
.PHONY : src/Sprite.s
# target to generate assembly for a file
src/Sprite.cpp.s:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/Sprite.cpp.s
.PHONY : src/Sprite.cpp.s
src/main.obj: src/main.cpp.obj
.PHONY : src/main.obj
# target to build an object file
src/main.cpp.obj:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/main.cpp.obj
.PHONY : src/main.cpp.obj
src/main.i: src/main.cpp.i
.PHONY : src/main.i
# target to preprocess a source file
src/main.cpp.i:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/main.cpp.i .PHONY : src/main.cpp.i
src/main.s: src/main.cpp.s
.PHONY : src/main.s
# target to generate assembly for a file
src/main.cpp.s:
$(MAKE) -f CMakeFiles\game_engine.dir\build.make CMakeFiles/game_engine.dir/src/main.cpp.s .PHONY : src/main.cpp.s
# Help Target
help:
@echo The following are some of the valid targets for this Makefile:
@echo ... all (the default if no target is provided)
@echo ... clean
@echo ... depend
@echo ... game_engine
@echo ... edit_cache
@echo ... rebuild_cache
@echo ... src/MainGame.obj
@echo ... src/MainGame.i
@echo ... src/MainGame.s
@echo ... src/Sprite.obj
@echo ... src/Sprite.i
@echo ... src/Sprite.s
@echo ... src/main.obj
@echo ... src/main.i
@echo ... src/main.s
.PHONY : help
#=============================================================================
# Special targets to cleanup operation of make.
# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
$(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles\Makefile.cmake 0
.PHONY : cmake_check_build_system
编辑(2):已解决
在确定 .lib 文件没有正确链接后,我决定通过它们的源包重新下载 GLEW 和 SDL2 库。它们都带有 cmake 作为构建选项,因此我能够访问静态库 (.a) + 导入库 (.dll.a)。
我用这些库替换了 lib 的内容并更新了我的 CMakeLists.txt 文件以包含它们以及 OpenGL 库:
cmake_minimum_required(VERSION 3.11.4)
# Project Name
project (game_engine)
# Retrieve all source files
set(SOURCES
"${CMAKE_SOURCE_DIR}/src/main.cpp"
"${CMAKE_SOURCE_DIR}/src/MainGame.cpp"
"${CMAKE_SOURCE_DIR}/src/Sprite.cpp")
add_executable(game_engine ${SOURCES})
find_package(OpenGL REQUIRED)
target_include_directories(game_engine PUBLIC
include
include/SDL2
include/GL
${OPENGL_INCLUDE_DIRS})
target_link_libraries(game_engine
"${CMAKE_SOURCE_DIR}/lib/glew32.dll"
"${CMAKE_SOURCE_DIR}/lib/libglew32.a"
"${CMAKE_SOURCE_DIR}/lib/libglew32.dll.a"
"${CMAKE_SOURCE_DIR}/lib/libSDL2.dll"
"${CMAKE_SOURCE_DIR}/lib/libSDL2.dll.a"
"${CMAKE_SOURCE_DIR}/lib/libSDL2main.a"
"${CMAKE_SOURCE_DIR}/lib/libSDL2-static.a"
"${OPENGL_LIBRARIES}")
add_custom_command(TARGET game_engine POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_SOURCE_DIR}/lib/glew32.dll"
"${CMAKE_BINARY_DIR}")
add_custom_command(TARGET game_engine POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${CMAKE_SOURCE_DIR}/lib/libSDL2.dll"
"${CMAKE_BINARY_DIR}")
这解决了 undefined reference 的主要问题,但还剩下一个问题,即对 WinMain 的 undefined reference 。为了解决这个问题,我在包含“SDL.h”之前将 #define SDL_MAIN_HANDLED
添加到我的 main.cpp 文件中。
最佳答案
target_link_libraries需要 CMake 目标或库路径。
尝试改变
target_link_libraries(game_engine -L./lib)
与
target_link_libraries(game_engine
${CMAKE_SOURCE_DIR}/lib/libglew32.lib
${CMAKE_SOURCE_DIR}/lib/glew32s.lib
${CMAKE_SOURCE_DIR}/lib/SDL2.lib
${CMAKE_SOURCE_DIR}/lib/SDL2main.lib
${CMAKE_SOURCE_DIR}/lib/SDL2test.lib
)
其他一些建议:
GLOB
。参见 file(GLOB...)合理的文档include_directories
更喜欢使用 target_include_directories GLEW_LIBRARIES
这样的变量)会更健壮。关于c++ - CMake - 尝试构建项目时遇到 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51757203/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!