User forums > Using Code::Blocks

Using multiple files in C::B

(1/2) > >>

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.   

Navigation

[0] Message Index

[#] Next page

Go to full version