mingw-g++可以查找目录

发布于 2025-02-03 05:55:23 字数 4941 浏览 5 评论 0原文

我正在尝试在Linux上编译Windows程序,因此我通过Debian软件包管理器安装了W64-MingW32编译器。我制作了一个单独的cmakelists文件,其中选择了x86_64-w64-mingw32-g ++作为编译器。当我尝试运行构建脚本时,我会发现错误,因为它找不到我在项目中使用的库。这是我的cmake文件:

cmake_minimum_required (VERSION 3.5)

if (POLICY CMP0072)
    cmake_policy(SET CMP0072 NEW)
else (NOT POLICY CMP0072)
    message(STATUS "Could not use CMP0072 policy")
endif(POLICY CMP0072)

project(opengl-test LANGUAGES CXX)

set(CMAKE_CXX_COMPILER "/usr/bin/x86_64-w64-mingw32-g++")
set(CMAKE_C_COMPILER "/usr/bin/x86_64-w64-mingw32-gcc")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs)

set(CMAKE_FIND_ROOT_PATH "/usr/x86_64-w64-mingw32")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

set (source_dir "${PROJECT_SOURCE_DIR}/src/")
set (base_dir "${source_dir}/base/")
set (IMGUI_DIR "/usr/include/imgui")

set(GCC_COVERAGE_LINK_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

file(GLOB source_files "${source_dir}/*.cpp")
file(GLOB_RECURSE base_files "${base_dir}/*.cpp")
file(GLOB imgui_files "${IMGUI_DIR}/*.cpp")

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)

add_executable(${PROJECT_NAME} ${source_files} ${imgui_files} ${base_files})

#target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
message(STATUS ${OPENGL_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PUBLIC ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})

cmakelists文件中的许多代码无能为力,因为我试图使它起作用,但没有任何作用。我把它留在里面,这样您就可以看到我尝试过的东西并且不起作用。 我可以运行此功能并进行编译,但是我会收到一个链接器错误。

CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.


-- /usr/lib/x86_64-linux-gnu/libOpenGL.so/usr/lib/x86_64-linux-gnu/libGLX.so/usr/lib/x86_64-linux-gnu/libGLU.so
-- Configuring done
CMake Warning at CMakeLists.txt:38 (add_executable):
  Cannot generate a safe runtime search path for target opengl-test because
  files in some directories may conflict with libraries in implicit
  directories:

    runtime library [libGLEW.so.2.1] in /usr/lib64 may be hidden by files in:
      /usr/lib/x86_64-linux-gnu

  Some of these libraries may not be found correctly.


-- Generating done
-- Build files have been written to: /home/user/Documents/CPP-Stuff/Scrap-Framework
[29/29] Linking CXX executable opengl-test
FAILED: opengl-test 
: && /usr/bin/x86_64-w64-mingw32-g++  -g   CMakeFiles/opengl-test.dir/src/main.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_demo.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_draw.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_impl_glfw.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_impl_opengl3.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_tables.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_widgets.cpp.o CMakeFiles/opengl-test.dir/src/base/Application/Application.cpp.o CMakeFiles/opengl-test.dir/src/base/Application/Window.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/Cubemap.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/Texture.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/UniformBuffer.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/VertexBuffer.cpp.o CMakeFiles/opengl-test.dir/src/base/Input/Input.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/Material.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/Mesh.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/Model.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/ModelLoader.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/FPSCamera.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/MaterialManager.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/Renderbuffer.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/Renderer.cpp.o CMakeFiles/opengl-test.dir/src/base/Scene/Lights.cpp.o CMakeFiles/opengl-test.dir/src/base/Scene/Scene.cpp.o CMakeFiles/opengl-test.dir/src/base/Scene/Skybox.cpp.o CMakeFiles/opengl-test.dir/src/base/Shader/Shader.cpp.o CMakeFiles/opengl-test.dir/src/base/Shader/ShaderManager.cpp.o  -o opengl-test  -Wl,-rpath,/usr/lib/x86_64-linux-gnu -lglfw /usr/lib/x86_64-linux-gnu/libOpenGL.so /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib64/libGLEW.so && :
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lglfw
/usr/bin/x86_64-w64-mingw32-ld: /usr/lib/x86_64-linux-gnu/libOpenGL.so: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

包含文件位于/usr/include/code>中,但我也将它们放在/usr/x86_64-W64-Mingw32/include/codusth/code中。 有人知道如何解决此问题,还是在Linux上使用Windows编译的更好方法?

I'm trying to compile my program for Windows, on Linux, so I installed the w64-mingw32 compiler via the Debian package manager. I made a separate cmakelists file where I chose x86_64-w64-mingw32-g++ as the compiler. When I try to run my build script, I get errors where it can't find the libraries that I use in my project. This is my cmake file:

cmake_minimum_required (VERSION 3.5)

if (POLICY CMP0072)
    cmake_policy(SET CMP0072 NEW)
else (NOT POLICY CMP0072)
    message(STATUS "Could not use CMP0072 policy")
endif(POLICY CMP0072)

project(opengl-test LANGUAGES CXX)

set(CMAKE_CXX_COMPILER "/usr/bin/x86_64-w64-mingw32-g++")
set(CMAKE_C_COMPILER "/usr/bin/x86_64-w64-mingw32-gcc")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GNUInstallDirs)

set(CMAKE_FIND_ROOT_PATH "/usr/x86_64-w64-mingw32")
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

set (source_dir "${PROJECT_SOURCE_DIR}/src/")
set (base_dir "${source_dir}/base/")
set (IMGUI_DIR "/usr/include/imgui")

set(GCC_COVERAGE_LINK_FLAGS "")
set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

file(GLOB source_files "${source_dir}/*.cpp")
file(GLOB_RECURSE base_files "${base_dir}/*.cpp")
file(GLOB imgui_files "${IMGUI_DIR}/*.cpp")

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)

add_executable(${PROJECT_NAME} ${source_files} ${imgui_files} ${base_files})

#target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)
message(STATUS ${OPENGL_LIBRARIES})
target_link_libraries(${PROJECT_NAME} PUBLIC ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} ${GLEW_LIBRARIES})

A lot of the code in the cmakelists file doesn't do anything because I was trying to make it work but nothing was working. I left it in so you can see what I've tried and doesn't work.
I can run this and it compiles, but I get a linker error.

CMake Warning:
  No source or binary directory provided.  Both will be assumed to be the
  same as the current working directory, but note that this warning will
  become a fatal error in future CMake releases.


-- /usr/lib/x86_64-linux-gnu/libOpenGL.so/usr/lib/x86_64-linux-gnu/libGLX.so/usr/lib/x86_64-linux-gnu/libGLU.so
-- Configuring done
CMake Warning at CMakeLists.txt:38 (add_executable):
  Cannot generate a safe runtime search path for target opengl-test because
  files in some directories may conflict with libraries in implicit
  directories:

    runtime library [libGLEW.so.2.1] in /usr/lib64 may be hidden by files in:
      /usr/lib/x86_64-linux-gnu

  Some of these libraries may not be found correctly.


-- Generating done
-- Build files have been written to: /home/user/Documents/CPP-Stuff/Scrap-Framework
[29/29] Linking CXX executable opengl-test
FAILED: opengl-test 
: && /usr/bin/x86_64-w64-mingw32-g++  -g   CMakeFiles/opengl-test.dir/src/main.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_demo.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_draw.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_impl_glfw.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_impl_opengl3.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_tables.cpp.o CMakeFiles/opengl-test.dir/usr/include/imgui/imgui_widgets.cpp.o CMakeFiles/opengl-test.dir/src/base/Application/Application.cpp.o CMakeFiles/opengl-test.dir/src/base/Application/Window.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/Cubemap.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/Texture.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/UniformBuffer.cpp.o CMakeFiles/opengl-test.dir/src/base/GL/VertexBuffer.cpp.o CMakeFiles/opengl-test.dir/src/base/Input/Input.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/Material.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/Mesh.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/Model.cpp.o CMakeFiles/opengl-test.dir/src/base/Model/ModelLoader.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/FPSCamera.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/MaterialManager.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/Renderbuffer.cpp.o CMakeFiles/opengl-test.dir/src/base/Renderer/Renderer.cpp.o CMakeFiles/opengl-test.dir/src/base/Scene/Lights.cpp.o CMakeFiles/opengl-test.dir/src/base/Scene/Scene.cpp.o CMakeFiles/opengl-test.dir/src/base/Scene/Skybox.cpp.o CMakeFiles/opengl-test.dir/src/base/Shader/Shader.cpp.o CMakeFiles/opengl-test.dir/src/base/Shader/ShaderManager.cpp.o  -o opengl-test  -Wl,-rpath,/usr/lib/x86_64-linux-gnu -lglfw /usr/lib/x86_64-linux-gnu/libOpenGL.so /usr/lib/x86_64-linux-gnu/libGLX.so /usr/lib/x86_64-linux-gnu/libGLU.so /usr/lib64/libGLEW.so && :
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lglfw
/usr/bin/x86_64-w64-mingw32-ld: /usr/lib/x86_64-linux-gnu/libOpenGL.so: error adding symbols: file in wrong format
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

The include files are located in /usr/include/, but I also put them in /usr/x86_64-w64-mingw32/include/.
Does anyone know how I can fix this, or a better way to compile for Windows on Linux?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过度放纵 2025-02-10 05:55:23

您需要构建您用于Windows使用的库,或找到预构建的库,例如 msysys2 repositories < /a>。您还需要将CMAKE指向这些库。

我已经制作了 quasi-msys2 可以自动化这两个。
示例用法:

# Install Clang, LLD, Wine. Then:
git clone https://github.com/holyblackcat/quasi-msys2
cd quasi-msys2/
make install _gcc _glfw _glew
env/shell.sh

# Build
cd your/project/location
mkdir build
cd build
cmake ..
make

# Run with Wine
./a.exe

这是我使用过的cmakelists.txt。我删除了交叉兼容的东西(自动处理)。

cmake_minimum_required (VERSION 3.5)

if (POLICY CMP0072)
    cmake_policy(SET CMP0072 NEW)
else (NOT POLICY CMP0072)
    message(STATUS "Could not use CMP0072 policy")
endif(POLICY CMP0072)

project(opengl-test LANGUAGES CXX)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)

add_executable(a 1.cpp)

message(STATUS ${OPENGL_LIBRARIES})
target_link_libraries(a PUBLIC ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} GLEW::GLEW)

我还将它们放入/usr/x86_64-w64-Mingw32/include/

您不应该手动修改目录。将它们留给您的包装经理。

You need to build the libraries you're using for Windows, or find prebuilt ones, e.g. in MSYS2 repositories. You also need to point CMake to those libraries.

I've made Quasi-MSYS2 to automate both.
Example usage:

# Install Clang, LLD, Wine. Then:
git clone https://github.com/holyblackcat/quasi-msys2
cd quasi-msys2/
make install _gcc _glfw _glew
env/shell.sh

# Build
cd your/project/location
mkdir build
cd build
cmake ..
make

# Run with Wine
./a.exe

Here is CMakeLists.txt I've used. I removed the cross-compilation stuff (which is handled automatically).

cmake_minimum_required (VERSION 3.5)

if (POLICY CMP0072)
    cmake_policy(SET CMP0072 NEW)
else (NOT POLICY CMP0072)
    message(STATUS "Could not use CMP0072 policy")
endif(POLICY CMP0072)

project(opengl-test LANGUAGES CXX)

find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)

add_executable(a 1.cpp)

message(STATUS ${OPENGL_LIBRARIES})
target_link_libraries(a PUBLIC ${GLFW_LIBRARIES} ${OPENGL_LIBRARIES} GLEW::GLEW)

I also put them in /usr/x86_64-w64-mingw32/include/

You shouldn't modify system include directories manually. Leave them to your package manager.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文