简体   繁体   English

在 Code::Blocks 上使用 64 位 MinGW 进行编译

[英]Compiling with 64-bit MinGW on Code::Blocks

I'm trying to set up my Code::Blocks installation to work with the SDL2 graphics API.我正在尝试设置我的 Code::Blocks 安装以使用 SDL2 图形 API。 As the documentation suggests, once I had everything installed and specified correctly, I tried the SDL_Init(SDL_INIT_EVERYTHING) test, as:正如文档所暗示的那样,一旦我安装并正确指定了所有东西,我就尝试了SDL_Init(SDL_INIT_EVERYTHING)测试,如下所示:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <SDL2/SDL.h>

int main (int argc, char *argv[])
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        printf("\nSDL2 could not be initialized! Code: %s\n", SDL_GetError());
    }

    return EXIT_SUCCESS;
}

However, I got the following errors:但是,我收到以下错误:

...in function 'SDL_Main':
undefined reference to `SDL_Init`
undefined reference to `SDL_GetError`
undefined reference to 'WinMain@16'

I looked these errors up and they seem to be due to CodeBlocks using a 32-bit compiler, but I'm using the 64-bit SDL2 library.我查看了这些错误,它们似乎是由于使用 32 位编译器的 CodeBlocks 造成的,但我使用的是 64 位 SDL2 库。 Which makes sense, as my linked options are:这是有道理的,因为我的链接选项是:

-lmingw32 -lSDL2main -lSDL2

So, I used MinGW's WinBuilds tool to download the 64-bit MinGW fork, which gave me a massive amount of files in a new /mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/... folder.因此,我使用 MinGW 的WinBuilds工具下载了 64 位 MinGW fork,它在新的/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/...文件夹中为我提供了大量文件. Then I went to CodeBlocks' Settings -> Compiler -> Toolchain Executables and pointed the respective compiler executables at the exe's in bin .然后我转到 CodeBlocks 的Settings -> Compiler -> Toolchain Executables并将相应的编译器可执行Settings -> Compiler -> Toolchain Executables指向bin中的 exe。 My first thought was to then change -lmingw32 to -lmingw64 , but this doesn't work.我的第一个想法是将-lmingw32更改为-lmingw64 ,但这不起作用。 I get the error:我收到错误:

cannot find -lmingw64

At this point I'm not sure what I've done correctly, incorrectly, what is broken/working etc. and would really appreciate any help possible.在这一点上,我不确定我做对了什么,不正确,什么坏了/工作等,并且非常感谢任何可能的帮助。

It may also be worth noting that I extracted the 64-bit compiler to C:\\MinGW and pointed CodeBlocks at C:\\MinGW\\bin as the compiler installation directory.可能还值得注意的是,我将 64 位编译器解压到C:\\MinGW并将 CodeBlocks 指向C:\\MinGW\\bin作为编译器安装目录。 When I click Auto-Detect , it seems happy enough and states it has automatically found the GCC compiler in that location.当我单击Auto-Detect ,它似乎很高兴并声明它已在该位置自动找到 GCC 编译器。 Therefore, it must be my -lmingw64 flag that is wrong, or something else I have specified.因此,一定是我的-lmingw64标志错误,或者我指定的其他内容。

Edit: The 64-bit compiler was set up and detected correctly but continued to produce errors relating to SDL functions:编辑:64 位编译器已正确设置并检测到,但继续产生与SDL函数相关的错误:

||=== Build: Debug in Initialization_Test (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `SDL_main':|
C:\C\SDL_C_Graphics\Init_Test\Initialization_Test\main.cpp|8|undefined reference to `_SDL_Init'|
C:\C\SDL_C_Graphics\Init_Test\Initialization_Test\main.cpp|10|undefined reference to `_SDL_GetError'|
C:\C\SDL_C_Graphics\Init_Test\Initialization_Test\main.cpp|10|undefined reference to `_printf'|
obj\Debug\main.o||In function `__tcf_0':|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream|74|undefined reference to `__ZNSt8ios_base4InitD1Ev'|
obj\Debug\main.o||In function `__static_initialization_and_destruction_0':|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream|74|undefined reference to `__ZNSt8ios_base4InitC1Ev'|
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream|74|undefined reference to `_atexit'|
C:\SDL_Dev\SDL2-2.0.12\x86_64-w64-mingw32\lib\libSDL2main.a(SDL_windows_main.o)||In function `main_getcmdline':|
\Users\valve\release\SDL\SDL2-2.0.12-source\foo-x64\..\src\main\windows\SDL_windows_main.c|71|undefined reference to `SDL_main'|
||error: ld returned 1 exit status|
||=== Build failed: 8 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Command CodeBlocks may be executing for compilation, in separate log window:命令代码块可能正在执行编译,在单独的日志窗口中:

x86_64-w64-mingw32-g++.exe -LC:\SDL_Dev\SDL2-2.0.12\x86_64-w64-mingw32\lib -LC:\SDL_Dev\SDL2-2.0.12\x86_64-w64-mingw32\lib -LC:\SDL_Dev\SDL_Img\SDL2_image-2.0.5\x86_64-w64-mingw32\lib -LC:\SDL_Dev\SDL_Mixer\SDL2_mixer-2.0.4\x86_64-w64-mingw32\lib -L"C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\lib" -o bin\Debug\Initialization_Test.exe obj\Debug\main.o  -lmingw32 -lSDL2main -lSDL2  -lmingw32 -lSDL2main -lSDL2.dll -luser32 -lgdi32 -lwinmm -ldxguid
C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file `obj\Debug\main.o' is incompatible with i386:x86-64 output

UPDATE Your last edit shows the most important bit of the output (it's incidentally also the first part of the output):更新您的最后一次编辑显示了输出中最重要的部分(顺便提一下,它也是输出的第一部分):

C:/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: i386 architecture of input file `obj\Debug\main.o' is incompatible with i386:x86-64 output

This is the linker telling you you are mixing 32 and 64-bit binaries.这是链接器告诉您您正在混合 32 位和 64 位二进制文​​件。 It seems you compiled your source file main.cpp into a 32-bit binary object obj\\Debug\\main.o .您似乎将源文件main.cpp编译为 32 位二进制对象obj\\Debug\\main.o You'll need to provide the commands leading to the creation of that file.您需要提供导致创建该文件的命令。


Some general remarks:一些一般性评论:

  1. The last symbol you're missing, WinMain@16 is a Windows-specific entry point that you "request" by compiling/linking with -mwindows and is typical/necessary for a native Win32 GUI application.您缺少的最后一个符号WinMain@16是您通过使用-mwindows编译/链接“请求”的特定于 Windows 的入口点,并且对于本机 Win32 GUI 应用程序来说是典型的/必需的。 It also seems linking SDL2main "causes" this.似乎链接SDL2main “导致”了这一点。

  2. The "32"s you see in various system libraries and compiler prefixes reflects the API you're targeting, which is called "Win32", even on 64-bit Windows.您在各种系统库和编译器前缀中看到的“32”反映了您所针对的 API,即所谓的“Win32”,即使在 64 位 Windows 上也是如此。

On to the error information you posted:关于您发布的错误信息:

The undefined references to C++ symbols imply you are linking with gcc but instead should be linking with g++ which automagically links in the C++ bits you need.对 C++ 符号的未定义引用意味着您正在使用gcc链接,但应该使用g++链接, g++会自动链接您需要的 C++ 位。 These missing symbols are needed because you included <iostream> which causes some global initialization to happen.需要这些缺少的符号是因为您包含了<iostream>这会导致一些全局初始化发生。

I cannot reproduce your linker error with MSYS2's build of SDL2.我无法使用 MSYS2 的 SDL2 构建重现您的链接器错误。 Your test code gives me the following output:您的测试代码给了我以下输出:

g++ main.cpp -o sdl_test -lSDL2main -lSDL2
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function `main':
D:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status

For which I can refer back to my point 1 above.为此,我可以参考上面的第 1 点。 But that is not your issue as it's first complaining about the SDL symbols.但这不是您的问题,因为它首先抱怨 SDL 符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM