MinGW's linker (ld.exe) gives dozens of errors regarding the glfw3.lib static library file. Unlike the Visual Studio's (link.exe)

huangapple go评论89阅读模式
英文:

MinGW's linker (ld.exe) gives dozens of errors regarding the glfw3.lib static library file. Unlike the Visual Studio's (link.exe)

问题

我没有使用任何IDE。

这是我的唯一的CMake文件:

cmake_minimum_required(VERSION 3.8)

project(App VERSION 1.3)

file(GLOB_RECURSE APP_SOURCES
   ${PROJECT_SOURCE_DIR}/*.cpp
   ${PROJECT_SOURCE_DIR}/*.c
)

# 将C++标准设置为C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 定义CMAKE_HAS_X
add_definitions(-DCMAKE_HAS_X)

if(WIN32)
    # 添加特定于Windows的配置
    add_executable(  
                    MyApp
                    ${APP_SOURCES}
                    ${CMAKE_SOURCE_DIR}/Installer/AppIcon.rc
                 )
    target_compile_definitions(MyApp PRIVATE CRT)
else()
   # 添加非Windows配置
   add_executable(
                    MyApp
                    ${APP_SOURCES}
                )
endif(WIN32)

# 安装程序
install(TARGETS MyApp)

install(
            FILES
            ${CMAKE_SOURCE_DIR}/freetype.dll
            ${CMAKE_SOURCE_DIR}/assimp-vc142-mtd.dll
            ${CMAKE_SOURCE_DIR}/OpenAL32.dll
            DESTINATION bin
        )

install(
            DIRECTORY
            ${CMAKE_SOURCE_DIR}/Resources
            DESTINATION bin
        )

# 为MyApp目标添加包含目录
target_include_directories(MyApp PRIVATE ${PROJECT_SOURCE_DIR}/Main/thirdparty/include/)
target_include_directories(MyApp PRIVATE ${PROJECT_SOURCE_DIR}/Main/thirdparty)
target_include_directories(MyApp PRIVATE ${PROJECT_SOURCE_DIR}/Main/src)

# 为MyApp目标添加链接目录
target_link_directories(MyApp PRIVATE 
    ${PROJECT_SOURCE_DIR}/Main/thirdparty/lib
    ${PROJECT_SOURCE_DIR}/Main/thirdparty/lib/freetype
    ${PROJECT_SOURCE_DIR}
)

# X11和GLFW的包含目录
include_directories(${X11_INCLUDE_DIR})
include_directories(${GLFW_INCLUDE_DIRS})

# MyApp目标的链接库
target_link_libraries(MyApp PRIVATE 
    freetype 
    assimp-vc142-mtd 
    OpenAL32 
    glfw3 
    ${X11_LIBRARIES} 
    ${GLFW_LIBRARIES}
)

如果我没有记错的话,我在mac环境中使用了以下cmake函数来处理相同的问题:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINKER_FLAGS} -I/usr/local/lib -framework Cocoa -framework IOKit -framework CoreVideo -framework OpenGL -lglfw3")

链接器错误消息:

(一半的错误消息被删除)

我正在使用以下命令:

生成构建文件:
cmake -G "MinGW Makefiles" ..

编译构建文件夹:
cmake --build .

Visual Studio的链接器(link.exe)可以成功链接二进制文件。

如何使MinGW链接二进制文件而不出现任何错误。

此外,还有一些关于ASSIMP的链接错误。

我尝试从Visual Studio的编译器切换到MinGW编译器。但是,MinGW的链接器无法处理GLFW库,不像Visual Studio的链接器。

英文:

I'm not using any IDE.

Here is my only cmake file :

cmake_minimum_required(VERSION 3.8)

project(App VERSION 1.3)

file(GLOB_RECURSE APP_SOURCES
   ${PROJECT_SOURCE_DIR}/*.cpp
   ${PROJECT_SOURCE_DIR}/*.c
)

# Set C++ standard to C++17
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


# Define CMAKE_HAS_X
add_definitions(-DCMAKE_HAS_X)


if(WIN32)
    # Add Windows-specific configurations
    add_executable(  
                    MyApp
                    ${APP_SOURCES}
                    ${CMAKE_SOURCE_DIR}/Installer/AppIcon.rc
                 )
   
    target_compile_definitions(MyApp PRIVATE CRT)

else()
   
# Add non-Windows configurations
   add_executable(
                    MyApp
                    ${APP_SOURCES}
                )

endif(WIN32)





# Installer
install(TARGETS MyApp)

install(
            FILES
            ${CMAKE_SOURCE_DIR}/freetype.dll
            ${CMAKE_SOURCE_DIR}/assimp-vc142-mtd.dll
            ${CMAKE_SOURCE_DIR}/OpenAL32.dll
            DESTINATION bin
        )

install(
            DIRECTORY
            ${CMAKE_SOURCE_DIR}/Resources
            DESTINATION bin
        )




# Add include directories for MyApp target
target_include_directories(MyApp PRIVATE ${PROJECT_SOURCE_DIR}/Main/thirdparty/include/)
target_include_directories(MyApp PRIVATE ${PROJECT_SOURCE_DIR}/Main/thirdparty)
target_include_directories(MyApp PRIVATE ${PROJECT_SOURCE_DIR}/Main/src)

# Add link directories for MyApp target
target_link_directories(MyApp PRIVATE 
    ${PROJECT_SOURCE_DIR}/Main/thirdparty/lib
    ${PROJECT_SOURCE_DIR}/Main/thirdparty/lib/freetype
    ${PROJECT_SOURCE_DIR}
)

# Include directories for X11 and GLFW
include_directories(${X11_INCLUDE_DIR})
include_directories(${GLFW_INCLUDE_DIRS})

# Link libraries for MyApp target
target_link_libraries(MyApp PRIVATE 
    freetype 
    assimp-vc142-mtd 
    OpenAL32 
    glfw3 
    ${X11_LIBRARIES} 
    ${GLFW_LIBRARIES}
)

If my mind doesn't deceives me I handled the same issue in the mac environment using this cmake function :

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINKER_FLAGS} -I/usr/local/lib -framework Cocoa -framework IOKit -framework CoreVideo -framework OpenGL -lglfw3")

Linker error messages :

Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
Warning: corrupt .drectve at end of def file
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text+0x86): 
undefined reference to `Assimp::Exporter::Exporter()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text+0x9a): 
undefined reference to `Assimp::Exporter::GetExportFormatDescription(unsigned long long) const'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text+0x1bc): undefined reference to `Assimp::Exporter::~Exporter()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text+0x22c): undefined reference to `Assimp::Exporter::~Exporter()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text+0x273): undefined reference to `aiScene::aiScene()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text+0x497): undefined reference to `aiNode::aiNode()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(ExportModel.cpp.obj):ExportModel.cp:(.text$_ZN6Assimp8Exporter6ExportEPK7aiSceneRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_jPKNS_16ExportPropertiesE[_ZN6Assimp8Exporter6ExportEPK7aiSceneRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESB_jPKNS_16ExportPropertiesE]+0x5d): undefined reference to `Assimp::Exporter::Export(aiScene const*, char const*, char const*, unsigned int, Assimp::ExportProperties const*)'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(LoadModel.cpp.obj):LoadModel.cpp:(.text+0x116): undefined reference to `Assimp::Importer::Importer()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(LoadModel.cpp.obj):LoadModel.cpp:(.text+0x1a4): undefined reference to `Assimp::Importer::GetErrorString() const'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(LoadModel.cpp.obj):LoadModel.cpp:(.text+0x247): undefined reference to `Assimp::Importer::~Importer()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(LoadModel.cpp.obj):LoadModel.cpp:(.text+0x295): undefined reference to `Assimp::Importer::~Importer()'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(LoadModel.cpp.obj):LoadModel.cpp:(.text+0x88d): undefined reference to `aiMaterial::GetName() const'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\App.dir/objects.a(LoadModel.cpp.obj):LoadModel.cpp:(.text$_ZN6Assimp8Importer8ReadFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEj[_ZN6Assimp8Importer8ReadFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEj]+0x31): undefined reference to `Assimp::Importer::ReadFile(char const*, unsigned int)'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/window.obj):(.text$mn+0x8f4): undefined reference to `__security_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/window.obj):(.text$mn+0xc41): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/window.obj):(.text$mn+0xc54): undefined reference to `__security_check_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/window.obj):(.xdata+0x24): undefined reference to `__GSHandlerCheck'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/window.obj):(.rtc$IMZ+0x0): undefined reference to `_RTC_InitBase'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/window.obj):(.rtc$TMZ+0x0): undefined reference to `_RTC_Shutdown'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/monitor.obj):(.text$mn+0xa1e): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/monitor.obj):(.text$mn+0xf27): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/monitor.obj):(.text$mn+0x1689): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x1d68): undefined reference to `__security_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x1ec1): undefined reference to `__report_rangecheckfailure'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x2067): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x207a): undefined reference to `__security_check_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x33a6): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x35fc): undefined reference to `__security_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x3e35): undefined reference to `_RTC_CheckStackVars'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.text$mn+0x3e48): undefined reference to `__security_check_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.xdata+0x104): undefined reference to `__GSHandlerCheck'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/input.obj):(.xdata+0x1e8): undefined reference to `__GSHandlerCheck'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/init.obj):(.text$mn+0x394): undefined reference to `__security_cookie'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/init.obj):(.text$mn+0x3d9): undefined reference to `vsnprintf'
C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/PrimaryUser/source/repos/App/Main/thirdparty/lib/glfw3.lib(glfw.dir/Debug/init.obj):
collect2.exe: error: ld returned 1 exit status

(Half of the error message lines are deleted)

I'm using these commands :

Generate build files :
cmake -G "MinGW Makefiles" ..

Compile the build folder :
cmake --build .

Visual Studio's linker (link.exe) is linking the binary files successfuly.

How can I get the MinGW to link the binaries without a single error.

And also there are few ASSIMP linking errors tho.

I tried to switch from Visual Studio's compiler to the MinGW compiler. However, MinGW's linker couldn't handle the GLFW library unlike the Visual Studio's linker.

答案1

得分: 2

GNU ld 无法链接使用 MSVC 构建的 64 位静态库,但可以链接使用 MSVC 构建的 32 位静态库。

最好使用由 64 位 GCC 构建的 glfw3.a。你可以通过使用 MSYS2 来获取它,该平台提供了大量预构建的二进制库,包括 glfw3。

英文:

GNU ld cannot link 64-bit static libraries built with MSVC, where it can link 32-bit static libraries built with MSVC.

It's best to use a 64-bit GCC-built glfw3.a.
You can obtain this for example by using MSYS2, which has prebuilt binary packages for a gigantic amount of libraries, including glfw3.

huangapple
  • 本文由 发表于 2023年7月4日 21:15:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定