Matt
Matt

Reputation: 249

Code::Blocks - how to compile multiple source files

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".

Upvotes: 13

Views: 48753

Answers (6)

Mobilebass254
Mobilebass254

Reputation: 1

I had the same problem, you transfer complete programs to the console application as instructed but then you still get an error message. What worked for me: the core scripts shared between the programs should be rewritten (backspace, and select them from the menu that appears when you type in the first few characters), it's almost as if its to manually point out and call Code::block's attention to their shared kinship. LOL.

Upvotes: 0

qwr
qwr

Reputation: 10891

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

Upvotes: 1

user2100815
user2100815

Reputation:

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.

Upvotes: 1

user2438062
user2438062

Reputation: 221

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"

Upvotes: 22

Jake1164
Jake1164

Reputation: 12349

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.

Upvotes: 1

I Phantasm I
I Phantasm I

Reputation: 1681

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.

Upvotes: 1

Related Questions