Code::Blocks - how to compile multiple source files

╄→гoц情女王★ 提交于 2019-12-17 07:38:05

问题


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):

main.cpp:

#include <stdio.h>
#include "other.h"

int main (void)
{
    printf("%d\n", getfavoritenumber());

    return 0;
}

other.cpp

#include "other.h"

int getfavoritenumber(void)
{
    return 3;
}

other.h

#ifndef _OTHER_H_
#define _OTHER_H_

int getfavoritenumber(void);

#endif

Despite the fact that these three files should link 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".


回答1:


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"




回答2:


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.




回答3:


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.




回答4:


I did this:

  • I created a Console Project in 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.

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.

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.net and 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 the Toolchains 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.

And if at the end of the day this doesn't work, try the CB support forums at http://forums.codeblocks.org.




回答5:


Ensure all files (.h and .cpp) have been added to the project with Project>Add Files... or Project>Add FIles Recursively...



来源:https://stackoverflow.com/questions/5971206/codeblocks-how-to-compile-multiple-source-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!