在 C++ 中创建 main 函数的几个场景
Creating few scenarios of main function in C++
我正在创建更大的项目,我需要 3 或 4 个主要功能场景来测试 类 是否正常工作。我创建了 main.cpp 这样的文件
#define TEST2 //INTERACTIVE OR TEST1 OR TEST2 OR TEST3
#include <iostream>
#ifdef INTERACTIVE
#include "interactive.cpp"
#endif
#ifdef TEST1
#include "test1.cpp"
#endif
#ifdef TEST2
#include "test2.cpp"
#endif
#ifdef TEST3
#include "test3.cpp"
#endif
在每个测试文件中都是这样的
#include <iostream>
int main() {
//Code here
return 0;
}
我认为它应该可以工作,但是当我构建项目时出现链接器错误:
duplicate symbol _main in:
CMakeFiles/Project.dir/main.cpp.o
CMakeFiles/Project.dir/test2.cpp.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [Project] Error 1
make[2]: *** [CMakeFiles/Project.dir/all] Error 2
make[1]: *** [CMakeFiles/Project.dir/rule] Error 2
make: *** [Project] Error 2
怎么了?
您的问题是您仍在编译包含在 main.cpp
中的 cpp 文件 您需要从解决方案中删除所有这些文件,因为您只想构建 main.cpp
。如果你不这样做,那么两个 cpp 文件都会被编译,所以它们有相同的符号,所以你会得到一个重复的符号错误。
我正在创建更大的项目,我需要 3 或 4 个主要功能场景来测试 类 是否正常工作。我创建了 main.cpp 这样的文件
#define TEST2 //INTERACTIVE OR TEST1 OR TEST2 OR TEST3
#include <iostream>
#ifdef INTERACTIVE
#include "interactive.cpp"
#endif
#ifdef TEST1
#include "test1.cpp"
#endif
#ifdef TEST2
#include "test2.cpp"
#endif
#ifdef TEST3
#include "test3.cpp"
#endif
在每个测试文件中都是这样的
#include <iostream>
int main() {
//Code here
return 0;
}
我认为它应该可以工作,但是当我构建项目时出现链接器错误:
duplicate symbol _main in:
CMakeFiles/Project.dir/main.cpp.o
CMakeFiles/Project.dir/test2.cpp.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [Project] Error 1
make[2]: *** [CMakeFiles/Project.dir/all] Error 2
make[1]: *** [CMakeFiles/Project.dir/rule] Error 2
make: *** [Project] Error 2
怎么了?
您的问题是您仍在编译包含在 main.cpp
中的 cpp 文件 您需要从解决方案中删除所有这些文件,因为您只想构建 main.cpp
。如果你不这样做,那么两个 cpp 文件都会被编译,所以它们有相同的符号,所以你会得到一个重复的符号错误。