我是一个相当小的爱好项目,我们正在创建一个游戏,或者至少尝试这样做......我们面临的主要问题是我们能够在MacOS和Linux上编译项目,但不是在Windows上。
我们使用GLFW作为OpenGL的界面,因此我们需要在屏幕上显示任何内容。问题是当我们告诉Make做它的事情时,它会编译每个单独的模块但由于未定义的引用而无法链接它们。虽然这可能有意义,但奇怪的是这些未知的引用都有前缀__imp_
,这在源代码中不存在。示例:我们使用方法glfwInit
,但在尝试制作项目时,我们得到了:graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0xa): undefined reference to `_imp__glfwInit'
做了一些研究后,我发现This SO Question我尝试了答案。它失败了,因为编译后的唯一lib是libglfw3.a
,dll没有dll或提示。相应地重命名此文件没有帮助,也没有放置预编译的二进制文件。删除此文件会导致重新编译并重新出现,因此请清楚地知道它在哪里。
我们也试过切换到clang,因为它可能有一个更智能的链接器,但它不想在第一时间编译任何东西。
我对此进行了大量研究,我找到了无数的SO答案和论坛帖子,其中有相似甚至相同的问题,每个人都有不同的解决方案,但没有一个能为我们工作。
要生成Makefile,我们在主子目录中使用CMake和以下脚本(不用担心,它从上面调用,到目前为止我们已经工作了):
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(graphicsengine)
add_subdirectory(fileSys)
add_subdirectory(networking)
add_subdirectory(game)
add_subdirectory(glfw-3.2.1)
include_directories(glfw-3.2.1/include)
if (UNIX AND NOT APPLE)
find_package(OpenGL REQUIRED)
include_directories(${OPENGL_INCLUDE_DIR})
endif()
add_executable(${PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp")
target_link_libraries(${PROJECT_NAME} graphicsengine fileSys networking game)
if (APPLE)
target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
endif()
if (WIN32)
target_link_libraries(${PROJECT_NAME} opengl32 gdi32)
endif()
if (UNIX AND NOT APPLE)
target_link_libraries(${PROJECT_NAME} ${OPENGL_gl_LIBRARY})
endif()
target_link_libraries(${PROJECT_NAME} glfw)
install(TARGETS ${PROJECT_NAME} DESTINATION ${INSTALL_DIR})
其余重要的cmake调用位于父目录
中以下是自定义构建脚本调用的cmake / make的完整日志:
debug
release
-- The CXX compiler identification is GNU 5.3.0
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe
-- Check for working CXX compiler: D:/MinGW/bin/g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- The C compiler identification is GNU 5.3.0
-- Check for working C compiler: D:/MinGW/bin/gcc.exe
-- Check for working C compiler: D:/MinGW/bin/gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Could NOT find Vulkan (missing: VULKAN_INCLUDE_DIR)
-- Looking for dinput.h
-- Looking for dinput.h - not found
-- Looking for xinput.h
-- Looking for xinput.h - not found
-- Performing Test _GLFW_HAS_DEP
-- Performing Test _GLFW_HAS_DEP - Success
-- Performing Test _GLFW_HAS_ASLR
-- Performing Test _GLFW_HAS_ASLR - Success
-- Performing Test _GLFW_HAS_64ASLR
-- Performing Test _GLFW_HAS_64ASLR - Failed
-- Using Win32 for window creation
-- Configuring done
-- Generating done
-- Build files have been written to: D:/cpp-neon/Workspace/Supermerged
--------------MAKE STARTS HERE--------------
[ 60%] Built target glfw
[ 68%] Built target graphicsengine
[ 76%] Built target fileSys
[ 84%] Built target networking
[ 92%] Built target game
[ 96%] Linking CXX executable Submerged_PR0.exe
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0xa): undefined reference to `_imp__glfwInit'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x45): undefined reference to `_imp__glfwCreateWindow'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x55): undefined reference to `_imp__glfwTerminate'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x64): undefined reference to `_imp__glfwMakeContextCurrent'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x71): undefined reference to `_imp__glfwWindowShouldClose'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x98): undefined reference to `_imp__glfwSwapBuffers'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0x9f): undefined reference to `_imp__glfwPollEvents'
graphicsengine/libgraphicsengine.a(graphics.cpp.obj):graphics.cpp:(.text+0xa8): undefined reference to `_imp__glfwTerminate'
collect2.exe: error: ld returned 1 exit status
source\CMakeFiles\Submerged_PR0.dir\build.make:101: recipe for target 'source/Submerged_PR0.exe' failed
mingw32-make[2]: *** [source/Submerged_PR0.exe] Error 1
CMakeFiles\Makefile2:102: recipe for target 'source/CMakeFiles/Submerged_PR0.dir/all' failed
mingw32-make[1]: *** [source/CMakeFiles/Submerged_PR0.dir/all] Error 2
Makefile:128: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Make Failed, Aborting Install
PS D:\cpp-neon\Workspace\Supermerged>
如果你想知道,这是我们的项目结构:
上面的CMakeLists.txt就是您在/ source
中看到的那个其他模块以相同的方式构建,除了glfw,你可以在他们的网站上找到它
在这个时候,我们或多或少地厌倦了寻找解决方案,我们只是尝试一些东西,直到它有点工作,但最终导致了一切本来就不应该存在的东西,我们所有人都知道怎么结束...... (咳嗽)这些分支中的哪一个是我做了另外做的事情的那个?(咳)我们希望你们中的一些人(所以)可能有比我们任何人更好的方法来找到解决方案。随意提出任何可能有助于您找到有用的东西的其他内容!
提前致谢!
名称:sDev
答案 0 :(得分:0)
我们找到了它!
问题实际上在我们的graphics.hpp
:
我们有一行#define GLFW_DLL
哪个错了,因为它必须是
#define GLFW_STATIC
现在可行了