C++ 如何从代码块中的 .cpp 文件创建 .exe?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8450121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:35:06  来源:igfitidea点击:

How do I create a .exe from a .cpp file in Code Blocks?

c++codeblocks

提问by Dalkius

I've just started learning C++, and to display the outputs of code I found this method. This worked when I first compiled Structure of a Programme.cpp:

我刚开始学习 C++,为了显示代码的输出,我发现了这个方法。当我第一次编译 Programme.cpp 的结构时,这是有效的:

#include <iostream>

using namespace std;

int main ()
{
    cout << "Hello World!";
    return 0;
}

It gave me a .exe, which I opened, ran, and got a lovely 'Hello World!' appearing, but when I tried compiling a second one, Variables.cpp:

它给了我一个 .exe,我打开、运行它,然后得到一个可爱的“Hello World!” 出现,但是当我尝试编译第二个时,Variables.cpp:

#include <iostream>
using namespace std;

int main ()
{
    int a, b;
    int result;

    a=5;
    b=2;
    a=a+1;
    result=a-b;

    cout << result;

    return 0;
}

I didn't get a .exe at all, so couldn't work out how to open it. I tried re-compiling Structure of a Programme.cpp (after deleting all the associated files), but now that won't create a .exe anymore, either. The only files created are Structure of a Programme.o and Variables.o (in a sub-directory obj\Debug).

我根本没有得到 .exe,所以不知道如何打开它。我尝试重新编译 Programme.cpp 的结构(删除所有相关文件后),但现在也不会再创建 .exe 了。创建的唯一文件是 Programme.o 和 Variables.o 的结构(在子目录 obj\Debug 中)。

The only question I could find that seemed similar was this, but the problem seems to be slightly different, and I tried deleting one of the files (so there was only one of Structure of a Programme.cpp or Variables.cpp in the folder) and I still had the same result.

我能找到的唯一一个看起来相似的问题是this,但问题似乎略有不同,我尝试删除其中一个文件(因此文件夹中只有 Structure of a Programme.cpp 或 Variables.cpp 之一)我仍然有同样的结果。

Also, there were no compiler errors with either file, and I don't think I changed any options in Code Blocks between Structure of a Programme working and everything not working.

此外,这两个文件都没有编译器错误,而且我认为我没有在程序结构正常工作和一切不工作之间更改代码块中的任何选项。

Thanks,

谢谢,

Dalkius

达尔基乌斯

edit: Build logs:

编辑:构建日志:

Compiling: Structure of a Programme.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

Compiling: Variables.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

edit 2: 'Full Commandline' build logs:

编辑 2:“完整命令行”构建日志:

Build started on: 14-12-2011 at 07:57.39
Build ended on: 14-12-2011 at 08:01.03
-------------- Clean: Debug in cplusplus.com Tutorial ---------------
Done. 
mingw32-g++.exe -Wall -g -c "D:\My Documents##代码##HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial\Structure of a Programme.cpp" -o "obj\Debug\Structure of a Programme.o"
Process terminated with status 0 (0 minutes, 1 seconds)
0 errors, 0 warnings
mingw32-g++.exe -Wall -g -c "D:\My Documents##代码##HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial\Variables.cpp" -o obj\Debug\Variables.o
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings

回答by greatwolf

From looking at your updated build log, it appears the linking step isn't being performed to generate the final executable. There are a couple of things you can check and some ideas to try out:

从查看更新的构建日志来看,似乎没有执行链接步骤来生成最终的可执行文件。您可以检查以下几项内容,并尝试一些想法:

  • Make sure the linker executable and proper path is set so C::B can locate it. For MinGW the linker is invoked through the compiler driver named 'g++.exe'.
  • Check that 'Console application' is selected under 'Type'.
  • If everything looks okay but it still doesn't link try creating a new blank consoleproject. Add the existing files to that project and try building it.
  • Try building it manually from a 'cmd' command prompt to make sure the toolchain itself is functioning. You should find a 'mingwvars.bat' script under your mingw install. Run that script to open up a proper commandline environment. Do a simple test compile using this command:
  • 确保链接器可执行文件和正确路径已设置,以便 C::B 可以找到它。对于 MinGW,链接器是通过名为“g++.exe”的编译器驱动程序调用的。
  • 检查是否在“类型”下选择了“控制台应用程序”。
  • 如果一切正常,但仍然无法链接,请尝试创建一个新的空白控制台项目。将现有文件添加到该项目并尝试构建它。
  • 尝试从“cmd”命令提示符手动构建它,以确保工具链本身正在运行。您应该在 mingw 安装下找到一个“mingwvars.bat”脚本。运行该脚本以打开适当的命令行环境。使用以下命令进行简单的测试编译:

cd "D:\My Documents\0HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial"
g++.exe -Wall -g Variables.cpp -o Variables.exe

cd "D:\My Documents\0HOME\Programming\C++\Code Blocks\cplusplus.com Tutorial"
g++.exe -Wall -g Variables.cpp -o Variables.exe

Lastly, this is what your log should approximately look like when it's building correctly: C::B build log

最后,这是您的日志在正确构建时的大致样子: C::B 构建日志

回答by Farid-ur-Rahman

EXE files are mostly build each time when you run the code. Try finding the exe file of your program where you have installed or copied the C++ program files.

每次运行代码时,EXE 文件大多是构建的。尝试找到您已安装或复制 C++ 程序文件的程序的 exe 文件。

回答by Weaver

I'm not too familiar with codeblocks, but I'll try to help by explaining what the compiler is doing. Those .o files it's creating are called Object Files. Compilation at a high level works as such:

我对代码块不太熟悉,但我会尝试通过解释编译器正在做什么来提供帮助。它创建的那些 .o 文件称为对象文件。高层次的编译是这样工作的:

1) Your source code get's compiled by a compiler.

1) 您的源代码由编译器编译。

2) The compiler will interpret your code and create an object (or .o file) for each file you have (in general anyways).

2)编译器将解释您的代码并为您拥有的每个文件(通常无论如何)创建一个对象(或 .o 文件)。

3) These files are then "linked" together in part of the compilation process known as "the linker".

3) 然后在称为“链接器”的部分编译过程中将这些文件“链接”在一起。

4) Finally, the linker puts out your .exe file.

4) 最后,链接器输出您的 .exe 文件。

There's of course more to this (such as library files, pre-compiled dlls, pre-processing, etc.) but for your purposes you can think of it like above as you're just starting out.

当然还有更多相关内容(例如库文件、预编译的 dll、预处理等),但出于您的目的,您可以像上面那样考虑,因为您刚刚开始。

My guess would be you may have accidentally modified something with codeblocks linker, or it is looking in the wrong place to link the files - or even the linker is throwing an error (though most IDEs inform you of this). Again, I'm unfortunately not too familiar with codeblocks.

我的猜测是您可能不小心使用代码块链接器修改了某些内容,或者它在错误的位置链接文件 - 甚至链接器抛出错误(尽管大多数 IDE 会通知您这一点)。同样,不幸的是,我对代码块不太熟悉。

If there is any way in codeblocks to trigger a "clean" you should also try that and try rebuilding. This will remove (clean) up any old files that may still be there from last build.

如果代码块中有任何方法可以触发“清理”,您也应该尝试并尝试重建。这将删除(清理)上次构建中可能仍然存在的所有旧文件。

回答by Nawa

After building your program, in Build log you can see 'Executing:' where you can find the path of .exe file which your program just created.

构建程序后,在构建日志中,您可以看到“正在执行:”,您可以在其中找到程序刚刚创建的 .exe 文件的路径。