【发布时间】:2021-09-07 02:19:41
【问题描述】:
我设置了 CLion、MinGW 并将适当的 freeglut 文件添加到 MinGW。
现在,我正在尝试运行这段代码:
/*
* GL01Hello.cpp: Test OpenGL C/C++ Setup
*/
#include <windows.h> // For MS Windows
#include <GL/glut.h> // GLUT, includes glu.h and gl.h
/* Handler for window-repaint event. Call back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer
// Draw a Red 1x1 Square centered at origin
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.5f, -0.5f); // x, y
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glFlush(); // Render now
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutCreateWindow("OpenGL Setup Test"); // Create a window with the given title
glutInitWindowSize(320, 320); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutDisplayFunc(display); // Register display callback handler for window re-paint
glutMainLoop(); // Enter the infinitely event-processing loop
return 0;
}
...这是我收到的控制台消息:
====================[ Build | test | Default ]==================================
"D:\CLion 2021.1.2\bin\cmake\win\bin\cmake.exe" --build D:\test\cmake-build-default --target test -- -j 9
[ 25%] Built target libfreeglut_static.a
[ 50%] Built target libfreeglut.a
Scanning dependencies of target test
[ 75%] Building CXX object CMakeFiles/test.dir/main.cpp.obj
[100%] Linking CXX executable test.exe
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x25): undefined reference to `__imp___glutInitWithExit'
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x4c): undefined reference to `__imp___glutCreateWindowWithExit'
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x72): undefined reference to `__imp___glutCreateMenuWithExit'
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x192): undefined reference to `__imp_glutInitWindowSize'
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1a5): undefined reference to `__imp_glutInitWindowPosition'
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1b5): undefined reference to `__imp_glutDisplayFunc'
CMakeFiles\test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x1be): undefined reference to `__imp_glutMainLoop'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\test.dir\build.make:106: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:99: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:106: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:136: test] Error 2
我的 CMakeLists.txt 看起来像这样:
cmake_minimum_required(VERSION 3.19)
project(test)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -pthread -fpermissive")
add_executable(test main.cpp)
link_directories(${OPENGL_gl_LIBRARY})
add_library(libfreeglut.a SHARED ${CMAKE_SOURCE_DIR}/lib/)
set_target_properties(libfreeglut.a PROPERTIES LINKER_LANGUAGE CXX)
add_library(libfreeglut_static.a STATIC ${CMAKE_SOURCE_DIR}/lib/)
set_target_properties(libfreeglut_static.a PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(test libfreeglut.a libfreeglut_static.a ${OPENGL_gl_LIBRARY})
我确定问题出在链接库上,但我不明白为什么某些 freeglut 函数无论如何都能正常工作,这让我觉得我一定在某个地方做了什么。
突出显示的两个函数工作得很好,它们是在 freeglut 库之一中定义的:
更新:
根据 ben10 的建议,我在修改 CMake 文件中的任何内容之前启用了CMAKE_EXPORT_COMPILE_COMMANDS,并生成了这个:
[
{
"directory": "D:/test/cmake-build-default",
"command": "D:\\minGW\\mingw64\\bin\\x86_64-w64-mingw32-g++.exe -std=c++14 -pthread -fpermissive -std=gnu++14 -o CMakeFiles\\test.dir\\main.cpp.obj -c D:\\test\\main.cpp",
"file": "D:/test/main.cpp"
}
]
之后我按照ben10的推荐修改了CMakeLists.txt,现在编译不出来了。错误信息如下:
"D:\CLion 2021.1.2\bin\cmake\win\bin\cmake.exe" -DCMAKE_MAKE_PROGRAM=D:/minGW/mingw64/bin/mingw32-make.exe -DCMAKE_C_COMPILER=D:/minGW/mingw64/bin/x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER=D:/minGW/mingw64/bin/x86_64-w64-mingw32-g++.exe -G "CodeBlocks - MinGW Makefiles" D:\test
-- Configuring done
CMake Error at CMakeLists.txt:22 (add_library):
No SOURCES given to target: libfreeglut
CMake Error at CMakeLists.txt:23 (add_library):
No SOURCES given to target: libfreeglut_static
CMake Generate step failed. Build files cannot be regenerated correctly.
[Failed to reload]
既然它说 libfreeglut 没有来源(尽管它指向CMAKE_SOURCE_DIR/lib/),这是否意味着我搞砸了向 MinGW 添加 glut?我重新安装了 MinGW 和 glut,但仍然得到相同的结果。
【问题讨论】:
-
我认为您不想同时链接此
libfreeglut_static.a和libfreeglut.a。我相信libfreeglut.a使用 dll 而libfreeglut_static.a没有。 -
我会先做一些事情:1)删除库名称中的 .a,你有一个共享库和一个静态库,让 cmake 为你添加扩展名:@987654334 @ (我什至会考虑考虑
lib前缀(在某些平台上也添加了)。2)我会做类似以下的事情:设置CMAKE_VERBOSE_MAKEFILE如果你使用的是make(或忍者)并让 cmake 输出详细的构建命令,也许它们会帮助你弄清楚发生了什么 -
错误消息表明您的构建需要链接到 freeglut 库 FWIW 的动态 (DLL) 版本。
-
另外,我认为您需要与
GLUT::GLUT链接(如果我正确理解了 GLUT 的 find_package) -
编辑后,
${CMAKE_SOURCE_DIR}/lib/的文件夹中究竟有什么?从compile_commands的外观看来,似乎没有为这些人建造任何东西。这有点令人困惑。你也可以试试before和CMAKE_VERBOSE_MAKEFILE 吗?并且,请提供一些关于它如何构建静态和共享库的输出
标签: c++ opengl cmake clion freeglut