C++ Code::Blocks - 如何编译多个源文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5971206/
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
Code::Blocks - how to compile multiple source files
提问by Matt
I'm trying to compile a program with multiple source files - two CPP files and a header file, with code::blocks. As an example, I have created the following three files (an example program created by someone else on another forum):
我正在尝试使用多个源文件编译一个程序 - 两个 CPP 文件和一个头文件,带有 code::blocks。例如,我创建了以下三个文件(由其他人在另一个论坛上创建的示例程序):
main.cpp:
主.cpp:
#include <stdio.h>
#include "other.h"
int main (void)
{
printf("%d\n", getfavoritenumber());
return 0;
}
other.cpp
其他.cpp
#include "other.h"
int getfavoritenumber(void)
{
return 3;
}
other.h
其他.h
#ifndef _OTHER_H_
#define _OTHER_H_
int getfavoritenumber(void);
#endif
Despite the fact that these three files shouldlink to each other, I receive the error "Linking stage skipped (build target has no object files to link)" when I try to build the project.
尽管这三个文件应该相互链接,但我在尝试构建项目时收到错误“链接阶段已跳过(构建目标没有要链接的目标文件)”。
What am I doing wrong? Trying to compile the individual files presents the error "That file isn't assigned to any target".
我究竟做错了什么?尝试编译单个文件会出现错误“该文件未分配给任何目标”。
回答by user2438062
Here is what worked for me:
这是对我有用的:
Go to the left panel that says projects, and right-click on .cpp file. Select properties, then go to build. Check the boxes under the heading Belongs in Targets: "Debug" and "Release"
转到显示项目的左侧面板,然后右键单击 .cpp 文件。选择属性,然后去构建。选中“属于目标”标题下的框:“调试”和“发布”
回答by Matt
I did this:
我这样做了:
I created a Console Projectin Code::Blocks
For each file i did
File|New
to create an empty file, added it to the project with the names you specified and pasted the relevant code from your question into each file.Compiled and ran the resulting executable.
我在Code::Blocks 中创建了一个控制台项目
对于我
File|New
为创建一个空文件所做的每个文件,使用您指定的名称将其添加到项目中,并将您的问题中的相关代码粘贴到每个文件中。编译并运行生成的可执行文件。
Everything worked as expected. If it doesn't work for you, please describe how you are creating the project. Code::Blocks absolutely needs a project - it doesn't work well with individual files. If you want that, use GCC from the command line.
一切都按预期进行。如果它不适合您,请描述您是如何创建项目的。Code::Blocks 绝对需要一个项目——它不适用于单个文件。如果需要,请从命令行使用 GCC。
Edit:
编辑:
It is generally a good idea to install the compiler separately from CB, which is really only an IDE. I am assuming we are on Windows here. Go to http://tdm-gcc.tdragon.netand download the latest GCC compiler from there. Check it works from the command line.
Then in CB go to
Settings|Compiler and Debugger
and select theToolchains executables
tab. Then navigate to the root of the directory where you installed the TDM GCC stuff (the root, not the bin directory within the root), and all should be well.
将编译器与 CB 分开安装通常是个好主意,CB 实际上只是一个 IDE。我假设我们在这里使用 Windows。转到http://tdm-gcc.tdragon.net并从那里下载最新的 GCC 编译器。从命令行检查它是否有效。
然后在 CB 中转到
Settings|Compiler and Debugger
并选择Toolchains executables
选项卡。然后导航到安装 TDM GCC 内容的目录的根目录(根目录,而不是根目录中的 bin 目录),一切都应该没问题。
And if at the end of the day this doesn't work, try the CB support forums at http://forums.codeblocks.org.
如果在一天结束时这不起作用,请尝试http://forums.codeblocks.org 上的 CB 支持论坛。
回答by I Phantasm I
I had a similar problem when creating my first multi source code project. i believe the problem you are having is not with the linking but with you #include statement for me the directory's were different to what i expected. to include the header file in a project i had to write #include "include/other.h"
have a look at how your folder system is constructed....if you could post what folders/directory`s you have in the project i might be able to give you a better answer.
在创建我的第一个多源代码项目时,我遇到了类似的问题。我相信您遇到的问题不在于链接,而在于您的#include 语句对我来说目录与我预期的不同。将头文件包含在我不得不写的项目中,#include "include/other.h"
看看你的文件夹系统是如何构建的....如果你能发布你在项目中拥有的文件夹/目录,我可能会给你一个更好的回答。
回答by Jake1164
I had a similar issue and found that if I just closed the project, created a new blank console application then imported the existing files things started to compile fine.
我有一个类似的问题,发现如果我刚刚关闭项目,创建一个新的空白控制台应用程序,然后导入现有文件,事情就会开始编译。
回答by qwr
Ensure all files (.h and .cpp) have been added to the project with Project>Add Files... or Project>Add FIles Recursively...
使用 Project>Add Files... 或 Project>Add FIles Recursively... 确保已将所有文件(.h 和 .cpp)添加到项目中。