Im writing my final project and i have everything in one .cpp file and i was wondering if someone could tell me in detail how to go about putting things in their own file and leaving the main function by itself in the main file. for example i have some edits that i would like to put into the resource folder and not to mention my other functions (im not sure which folder they would go in but im guessing also the resource folder)

please tell me the steps to getting this objective completed "its not mandatory but i would like to have a clean project...thx

how to do that will depend on the compiler, each one is a little different.

Do you mean you want to put the source files, such as *.cpp, *.c, and *.h, in different folders?

commented: very helpful +1

yes thats exactly what i want, and im using visual c++ 2005

I've tried that before (putting files in different folders) with that compiler and vc++ 2008 express. It can be done, but a little tricky.

For *.cpp and *.c files: open Solution Explorer, right-click Source Files, then in the dropdown menu select Add --> New Filter. That will add a new folder under Source Files folder. Do the same with with the Header Files folder. After that you can add existing files to those new folders -- right click on the folder then select Add --> Existing Item then navigate to the items you want to add to that folder.

When you compile the solution you have to be careful about the location of the includes that are in those *.cpp and *.c files because for locally created header files they won't be in the same place as they would have been had you just put the source files directly in the Source Files folder.

how would i include these files (.cpp, .h etc.) ?

so they work when i call the function from the main

when you add the files to the project as I previously explained and you select Build --> Build Solution the IDE will compile all the *.cpp and *.c files then link the all together into one executable program. Its really not at all that difficult to do.

In the file that contains main() function you will probably have to declare functions in other files as extern, if you don't do that then the compiler will complain about undeclared functions

// main

extern int foo(); // located in another *.cpp file

int main()
{
    foo(); // call the function in another file
}

thank you . you are very helpful :)


i marked as solved and added to your reputation, have a great night and thx again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.