Author Topic: Using multiple files in C::B  (Read 3283 times)

Offline jagdesign@cableone.net

  • Single posting newcomer
  • *
  • Posts: 6
Using multiple files in C::B
« on: July 18, 2020, 08:25:03 pm »
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
« Last Edit: July 18, 2020, 08:26:44 pm by jagdesign@cableone.net »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Using multiple files in C::B
« Reply #1 on: July 18, 2020, 09:22:06 pm »
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?

Offline jagdesign@cableone.net

  • Single posting newcomer
  • *
  • Posts: 6
Re: Using multiple files in C::B
« Reply #2 on: July 20, 2020, 07:23:43 pm »
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;
}

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Using multiple files in C::B
« Reply #3 on: July 20, 2020, 08:06:50 pm »
This is not related to C::B, you must ask this question in a C++ forum.

Offline jagdesign@cableone.net

  • Single posting newcomer
  • *
  • Posts: 6
Re: Using multiple files in C::B
« Reply #4 on: July 21, 2020, 04:02:30 am »
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.   

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Using multiple files in C::B
« Reply #5 on: July 21, 2020, 09:44:51 am »
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!]

Offline jagdesign@cableone.net

  • Single posting newcomer
  • *
  • Posts: 6
Re: Using multiple files in C::B
« Reply #6 on: July 24, 2020, 10:26:19 pm »
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?

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Using multiple files in C::B
« Reply #7 on: July 25, 2020, 09:48:34 am »
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!]