Calling self standing cpp files from a main cpp
-
I have a project that requires the use of multiple sorts and searches. I've pretty much coded the entire thing already, I have a few more unrelated issues to address that will be pretty easy to solve. I first coded the required Sorts and searches as self standing cpp files(that can be executed on their own). Now I have about twenty different files in cpps and headers, which is all well and good...I'm all for modularity, and I've hand-written out the code for headers for the sorts and searches (to split the cpps into headers and cpp implementation files to be called using include directives from the main cpp), but I thought it would be cool to call the self-standing cpp files from the main cpp and so far have not been able to do it. Is there anyone out there that can give me a line on how to do this, if it's even possible? I've tried several tactics to no avail.
-
I have a project that requires the use of multiple sorts and searches. I've pretty much coded the entire thing already, I have a few more unrelated issues to address that will be pretty easy to solve. I first coded the required Sorts and searches as self standing cpp files(that can be executed on their own). Now I have about twenty different files in cpps and headers, which is all well and good...I'm all for modularity, and I've hand-written out the code for headers for the sorts and searches (to split the cpps into headers and cpp implementation files to be called using include directives from the main cpp), but I thought it would be cool to call the self-standing cpp files from the main cpp and so far have not been able to do it. Is there anyone out there that can give me a line on how to do this, if it's even possible? I've tried several tactics to no avail.
assuming you've got sort.cpp and sort.h that include your sorting code then just
#include "sort.h"
at the top of main.cpp compile both .cpp files, link them together, and voila (if you're using VC, then just have the main.cpp and the sort.cpp and sort.h files included in your project, and it'll happily compile and link them) I'm not sure if this is what you're already doing. If so, what kind of errors are you actually getting? -- Help me! I'm turning into a grapefruit! -
assuming you've got sort.cpp and sort.h that include your sorting code then just
#include "sort.h"
at the top of main.cpp compile both .cpp files, link them together, and voila (if you're using VC, then just have the main.cpp and the sort.cpp and sort.h files included in your project, and it'll happily compile and link them) I'm not sure if this is what you're already doing. If so, what kind of errors are you actually getting? -- Help me! I'm turning into a grapefruit!No, that's not what I mean. It would be if I was using header files for the sorts and searches. As it stands, I have these files coded: Bubble.cpp, Radix.cpp, Selection.cpp, Binary.cpp, Sequential.cpp, and Insertion.cpp. These are all self-standing and can be compiled and run on their own. I can split them up into implementation cpps and declaration headers (creating classes), but I wanted to know if there is a way to avoid that process and call the self-standing cpps from a main.cpp, without calling headers for these specific algorithms and without coding functions for the algorithms in the main.cpp. Basically what the program does is it profiles the performance of each search and sort algorithm from a big-O perspective, based on what the user inputs(which sort/search to use, length of the vector array, how to get the data--from data file, randomly generated, or manually put in to the vector--, and which time constraint to use --seconds or minutes, dependent on the algorithm). It prints to screen and data file: seach/sort being used; length of vector; values in the array, before and after the sorts and searches; and profile data on the number of loops, exchanges, and the time it took for each algorithm that was chosen to perform it's duties. Everything's been coded already, except splitting the coded sorts and searches or coding the functions into main.cpp. I wanted to avoid splitting the cpps for the sorts and searches into declaration and implementation files or coding the sort/search algorithms into main. I've tried everything I could think of, and it looks like I'm going to have to split the files to do what I want, or copy and paste the code as functions in main.
-
No, that's not what I mean. It would be if I was using header files for the sorts and searches. As it stands, I have these files coded: Bubble.cpp, Radix.cpp, Selection.cpp, Binary.cpp, Sequential.cpp, and Insertion.cpp. These are all self-standing and can be compiled and run on their own. I can split them up into implementation cpps and declaration headers (creating classes), but I wanted to know if there is a way to avoid that process and call the self-standing cpps from a main.cpp, without calling headers for these specific algorithms and without coding functions for the algorithms in the main.cpp. Basically what the program does is it profiles the performance of each search and sort algorithm from a big-O perspective, based on what the user inputs(which sort/search to use, length of the vector array, how to get the data--from data file, randomly generated, or manually put in to the vector--, and which time constraint to use --seconds or minutes, dependent on the algorithm). It prints to screen and data file: seach/sort being used; length of vector; values in the array, before and after the sorts and searches; and profile data on the number of loops, exchanges, and the time it took for each algorithm that was chosen to perform it's duties. Everything's been coded already, except splitting the coded sorts and searches or coding the functions into main.cpp. I wanted to avoid splitting the cpps for the sorts and searches into declaration and implementation files or coding the sort/search algorithms into main. I've tried everything I could think of, and it looks like I'm going to have to split the files to do what I want, or copy and paste the code as functions in main.
Hi DP, Not sure if your asking for this, but how about using defines to cause the compiler to not compile the main() functions inside your library modules? In the future, you might want to break up your modules further to not include main inside each part, but make seperate test harnesses for each library. Hope that's kinda what you are after. In the below example, use a preprocessor define for your Radix standalone to compile with a local main(). Don't define the STANDALONE_MODULE_RADIX for your composite program that uses several of your sort libraries, and you won't have the problem of multiple included main(). Good luck! Nick. //Radix.cpp: #ifdef STANDALONE_MODULE_RADIX //Main int main() { //Setup your sort data here, but don't sort it. // ... // Now, all your data is set in structures, call your sort! MyRadixSort(); // Done, do some outputting. //... } #endif // Your actual sort code goes here... void MyRadixSort() { } ---------------------------------- Your header file for Radix.cpp: // Radix.h void MyRadixSort(); ---------------------------------- Ok, here is the your main file that you would use with a bunch of your libraries: // real_main.cpp: #include Radix.h void main() { // Set up your data for this composite program // Call your sorts... // ... MyRadixSort(); // All done! }
-
Hi DP, Not sure if your asking for this, but how about using defines to cause the compiler to not compile the main() functions inside your library modules? In the future, you might want to break up your modules further to not include main inside each part, but make seperate test harnesses for each library. Hope that's kinda what you are after. In the below example, use a preprocessor define for your Radix standalone to compile with a local main(). Don't define the STANDALONE_MODULE_RADIX for your composite program that uses several of your sort libraries, and you won't have the problem of multiple included main(). Good luck! Nick. //Radix.cpp: #ifdef STANDALONE_MODULE_RADIX //Main int main() { //Setup your sort data here, but don't sort it. // ... // Now, all your data is set in structures, call your sort! MyRadixSort(); // Done, do some outputting. //... } #endif // Your actual sort code goes here... void MyRadixSort() { } ---------------------------------- Your header file for Radix.cpp: // Radix.h void MyRadixSort(); ---------------------------------- Ok, here is the your main file that you would use with a bunch of your libraries: // real_main.cpp: #include Radix.h void main() { // Set up your data for this composite program // Call your sorts... // ... MyRadixSort(); // All done! }
Addendum: Been a long time....sorry for the delay. I couldn't get your idea to work, Nick. I guess I know too little to use what was suggested. Maybe that or there some restrictions on calling classes from within classes, structs or unions in MSVC++6.0. In any case, I recoded the thing using functions with templates in main() and called the default classes that are needed to use vectors and to return profiling data from there. It works, but I'm kinda disappointed in my lack of modularity within the program. Maybe I'll try to split it up later, when I investigate my options more (that's how it goes with an inexperienced newbie), but for now...."If it ain't broke, don't fix it." I appreciate the ideas...thanx for the help. Later. Eddie