0

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?

User-92
  • 374
  • 3
  • 13
  • *cannot find -lglfw* is looking for the library, not the header that describes the the interface to the library. You can muck with include folders until you're blue in the face AND the cows come home and get nowhere. You need to set the library path. – user4581301 May 31 '22 at 18:52
  • @user4581301 How would I do that? – User-92 May 31 '22 at 19:00
  • What I know about CMake you can fit *inside* the head of a pin, unfortunately. It's going to be `target_link_libraries`, I think, but most of the CMake error message is about how it's currently finding the Linux libraries, not the Windows libraries. You can probably hardcode the correct location, but that will wreck portability of the CMake file. If you don't know how to do it correctly, you'll have to wait for someone with actual CMake skill to swing by. I'm not that person. – user4581301 May 31 '22 at 19:06
  • CMake 3.8 was the first version to support C++17, so your minimum-required version of 3.5 is abjectly wrong. I would say to bump up to at least 3.11 so you don't need the `if (POLICY)` monkey-business. – Alex Reinking May 31 '22 at 19:12
  • Also, setting `CMAKE_CXX_COMPILER` after `project()` does _absolutely_ nothing and doesn't belong in the CMakeLists.txt _anyway_ (that variable should only be set in a toolchain file). – Alex Reinking May 31 '22 at 19:13
  • @AlexReinking: "setting `CMAKE_CXX_COMPILER` after `project()` does absolutely nothing" - No, such setting has an effect... but most likely not the effect which one's expect: https://stackoverflow.com/a/63944545/3440745 – Tsyvarev May 31 '22 at 21:32
  • @Tsyvarev -- oof that's even _worse_ than not doing anything! Thanks for letting me know! – Alex Reinking Jun 01 '22 at 01:25

1 Answers1

0

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.

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207