C++ 未定义对“imp”glClear'; G++版本:(第2版,由MSYS2项目构建)10.3.0 CMake版本:3.20.0 忍者版:1.10.2 GLFW版本:3.3.4 OS:Windows 10 IDE:VSCode
最近我决定从一个教程中学习CMakeC++ 未定义对“imp”glClear'; G++版本:(第2版,由MSYS2项目构建)10.3.0 CMake版本:3.20.0 忍者版:1.10.2 GLFW版本:3.3.4 OS:Windows 10 IDE:VSCode,c++,cmake,glfw,ninja,C++,Cmake,Glfw,Ninja,最近我决定从一个教程中学习CMake *确切地说,这个系列: 我在添加GLFW时遇到困难,因为此错误消息不断出现: 编辑:如果我不使用GLFW库中的任何内容,但仍然包含它,则它可以正常工作 错误消息 我试图使用预编译的二进制文件复制该系列中的每一位代码,但同样的错误不断出现 -主目录 编译.bat CMakeLists.txt Main.cpp -/src CMakeLists.txt 加法器 加法器 这里似乎有一个示例项目:;不必手动添加链接和包含目录,事实上,在链接目录的情况下,您很可能
*确切地说,这个系列: 我在添加GLFW时遇到困难,因为此错误消息不断出现: 编辑:如果我不使用GLFW库中的任何内容,但仍然包含它,则它可以正常工作 错误消息 我试图使用预编译的二进制文件复制该系列中的每一位代码,但同样的错误不断出现
-主目录 编译.bat CMakeLists.txt Main.cpp
-/src CMakeLists.txt 加法器 加法器
这里似乎有一个示例项目:;不必手动添加链接和包含目录,事实上,在链接目录的情况下,您很可能指定了错误的目录:输出通常会转到二进制目录,该目录最好与源目录不同。此外,我建议为构建目录选择一个不同于
bin
的名称。通常我希望在那里找到.dll
s和.exe
s,而不是整个解决方案;(我个人使用构建
)
C:\dev\CMGL>compile
C:\dev\CMGL>cmake -DGLFW_BUILD_DOCS=OFF -S . -B bin -G Ninja
-- The C compiler identification is GNU 10.3.0
-- The CXX compiler identification is GNU 10.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/mingw64/bin/cc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Using Win32 for window creation
-- Looking for dinput.h
-- Looking for dinput.h - found
-- Looking for xinput.h
-- Looking for xinput.h - found
-- Configuring done
-- Generating done
-- Build files have been written to: C:/dev/CMGL/bin
C:\dev\CMGL>cd bin
C:\dev\CMGL\bin>Ninja
[20/20] Linking CXX executable CMGL.exe
FAILED: CMGL.exe
cmd.exe /C "cd . && C:\mingw64\bin\c++.exe CMakeFiles/CMGL.dir/Main.cpp.obj -o CMGL.exe -Wl,--out-implib,libCMGL.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -LC:/dev/CMGL/src -LC:/dev/CMGL/vendor/GLFW/src src/libheader.a vendor/GLFW/src/libglfw3.a -lkernel32 -luser32 -lgdi32
-lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/CMGL.dir/Main.cpp.obj:Main.cpp:(.text+0xc9): undefined reference to `__imp_glClear'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
cmake -DGLFW_BUILD_DOCS=OFF -S . -B bin -G Ninja
cd bin
Ninja
pause
CMGL
pause
cd ../
cmake_minimum_required(VERSION 3.19.0)
project(CMGL)
add_executable(${PROJECT_NAME} Main.cpp)
add_subdirectory(src)
add_subdirectory(vendor/GLFW)
target_include_directories(${PROJECT_NAME}
PUBLIC vendor/GLFW/include
)
target_link_directories(${PROJECT_NAME}
PRIVATE src
PRIVATE vendor/GLFW/src
)
target_link_libraries(${PROJECT_NAME}
header
glfw
)
#include <iostream>
#include "src/adder.h"
#include <GLFW/glfw3.h>
int main()
{
std::cout << "test" << std::endl;
std::cout << add(3,4);
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
[submodule "vendor/GLFW"]
path = vendor/GLFW
url = https://github.com/glfw/glfw.git
add_library(header adder.cpp)
#include "adder.h"
int add(int x, int y)
{
return x + y;
}
int add(int x, int y);