How to support includelevel in Visual Studio 2010
At school we use c++/g++ compilers on Linux to support includelevel. This allow us to write an app, include a main that proves the functions work, and then to include that file in another program to use it's functions. The theory is that includelevel will block out code when you include so that you don't duplicate things (like having two mains). Is there a way to get Visual Studio 2010 to recognize includelevel? When compiling, it simply says there are two mains (which there are, but one is blocked off with include level) and fails to compile. I have been just commenting out code in the included, so that it compiles in VS2010, then uncommenting when pushing to the school server (Linux) to compile for submission. Ex: // file: sort.cpp
#include <iostream>
using namespace std;
void BubbleSort(int arr[], int numitems, int &bcost);
/开发者_运维知识库/ Fancy sorting function description
#if __INCLUDE_LEVEL__ < 1
int main()
{
//fancy program that proves the sorting function works
return 0;
}
#endif
void BubbleSort(int arr[], int numitems, int &bcost)
{
// Fancy sorting function code
}
----------------------------------------------------------
// file: myapp.cpp
#include <iostream>
#include "sort.cpp"
using namespace std;
int main()
{
//fancy application code that uses functions from the sort program
return 0;
}
What I believe you are doing is writing a test(s) in the same source file as the functions they test.
While this may be handy for a simple program, once it becomes more complicated the standard practice is to extract out the test functions into a seperate source file, and compile it into a seperate test executable (or DLL in some cases).
A simple structure that I've used before is to have a library project that implements the core functionality, have another project that contains the main function and compiles to the 'real' program, and have a third project that contains the tests and its own main function that runs them.
It's also non-standard to include a source/implementation (.cpp) file from another.
精彩评论