News:

The new Release 25.03 is out! You can download binaries for Windows and many major Linux distros here .

Main Menu

Using multiple files in C::B

Started by jagdesign@cableone.net, July 18, 2020, 08:25:03 PM

Previous topic - Next topic

jagdesign@cableone.net

Hey,
I'm just trying to learn C++ and C::B. I know that one program can be written to do this. My lessons say that I can use mutliple files with C::B (they may be called split files I'm not sure)  I've tried forward declaring and compiling both files but can't get this to work.  Reference lesson 2.8 Learning CPP.com.

Can someone please help?  What am I doing wrong? 

Basically I'm trying to use two simple files together and compile them. 

File names are main.cpp and add.cpp.  How do I do this properly with C::B?
Attached are the screen shots

Miguel Gimenez

You should tell us the error you get and the contents of add.ccpp, but it is better if you post a full rebuild log, see:

http://wiki.codeblocks.org/index.php/FAQ-Compiling_(errors)#Q:_How_do_I_troubleshoot_a_compiler_problem.3F

Do you have a main() function in add.cpp?

jagdesign@cableone.net

I'm trying to use multi files in a lesson, using CB 20.3 in W10.
They say, separate the add and main functions from the file below to do this.  Then compile them.

This doesn't compile as the author shows.  He says use a new file for the add function. Is it somehow possible ? How do I go about doing it?

The original is shown  with forward declaration next.
I've separated the two file below the************ lines if that will help to copy/paste.

#include <iostream>

int add(int x, int y); // forward declaration of add() (using a function prototype)

int main()
{
    std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n'; // this works because we forward declared add() above
    return 0;
}

int add(int x, int y) // even though the body of add() isn't defined until here
{
    return x + y;
}

************************************
add.cpp

int add(int x, int y) // even though the body of add() isn't defined until here
{
    return x + y;
************************************
main.cpp

#include <iostream>

int main()
{
    std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n'; // this works because we forward declared add() above
    return 0;
}

Miguel Gimenez

This is not related to C::B, you must ask this question in a C++ forum.

jagdesign@cableone.net

I beg your pardon,  it is relative to C::B
I realize the code shown is simple C++ but how does one run multi files using C::B.   

oBFusCATed

Just create a console or an empty project and start adding files.
And create an add.h file where you declare your function.
If you still cannot make it work please post the full build log as already requested.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

jagdesign@cableone.net

Thanks,  I managed to get this to work by creating a new Project. 
How ever that leaves me with other questions.

If now want to create a new projects to C::B the new project name gets grouped under any project you have under the management window.  How can I clear the workspace, and the project files that are already there?   Do I have to open Windows 10 and delete all the files there?

oBFusCATed

Use File -> close project or File -> close workspace. In the latter case you could click the no button when asked to save the default workspace.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]