c++ - 如何修复 Clion 中目标错误的配方?

标签 c++ cmake clion

我在 clion 中执行程序时收到以下错误。

"C:\Program Files\JetBrains\CLion 2019.2.3\bin\cmake\win\bin\cmake.exe" --build C:\Users\two43\CLionProjects\cse\cmake-build-debug --target cse -- -j 2
[ 25%] Linking CXX executable cse.exe
CMakeFiles\cse.dir\build.make:114: recipe for target 'cse.exe' failed
c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\cse.dir/objects.a(lecture-2.cpp.obj): in function `main':
C:/Users/two43/CLionProjects/cse/lecture-2.cpp:24: multiple definition of `main'; CMakeFiles\cse.dir/objects.a(lecture-1.cpp.obj):C:/Users/two43/CLionProjects/cse/lecture-1.cpp:19: first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [cse.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/cse.dir/all] Error 2
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/cse.dir/all' failed
mingw32-make.exe[1]: *** [CMakeFiles/cse.dir/rule] Error 2
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/cse.dir/rule' failed
mingw32-make.exe: *** [cse] Error 2
Makefile:117: recipe for target 'cse' failed

CMakeLists.txt 文件如下:

cmake_minimum_required(VERSION 3.15)
project(cse)
set(CMAKE_CXX_STANDARD 17)
add_executable(cse main.cpp lecture-1.cpp lecture-2.cpp)

最佳答案

您正在编译两个文件,cse/lecture-1.cppcse/lecture-2.cpp。两者都包含一个 main 函数。您不能构建具有多个 main 函数的单个可执行文件。

通过在您的 CMake 定义中两次使用 add_executable 使它们成为独立的可执行文件。如果您提供 CMake 定义,我会向您展示如何操作。

编辑:根据您的 CMake 定义,使用

cmake_minimum_required(VERSION 3.15)
project(cse)
set(CMAKE_CXX_STANDARD 17)
add_executable(lecture-1 main.cpp lecture-1.cpp)
add_executable(lecture-2 main.cpp lecture-2.cpp)

这假定 main.cpp,尽管它的名字,不包含 另一个 main 函数。如果是这样,您也需要一个单独的 add_executable

关于c++ - 如何修复 Clion 中目标错误的配方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59047947/

相关文章:

c++ - std::hash 是否保证在 stdlib 发行版中相同

c++ - C++中的整数字节交换

build - 如何从 cmake 中的列表中删除标记?

cmake - 如何将 cmake 指向库的特定目录?

c++ - 为什么 CLion 可以正确构建和链接 Qt,但不能运行我的可执行文件?

c++ - opencv中的Vec4i是什么

c++ - boost 1.66.0 : could not find boost libraries :boost_system, boost_filesystem,boost_thread,boost_date_time

c++ - 在 Clion 2017 中保存时自动删除尾随空格

c++ - 如何正确创建单例对象并在 C++ 中使用它?