Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code::Blocks - how to compile multiple source files

Tags:

c++

codeblocks

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

like image 328
Matt Avatar asked May 11 '11 22:05

Matt


People also ask

Can you have multiple source files in C++?

You can have multiple definitions of the same class in different files (or because you included the same header file in different source files), and C++ will check them all against each other to make sure that they are identical, and then will merge them into a single class definition in your program.

How do I run multiple cpp files?

You can simply place a forward declaration of your second() function in your main. cpp above main() . If your second. cpp has more than one function and you want all of it in main() , put all the forward declarations of your functions in second.


2 Answers

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"

like image 109
user2438062 Avatar answered Sep 27 '22 19:09

user2438062


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.

like image 24
I Phantasm I Avatar answered Sep 27 '22 19:09

I Phantasm I