【问题标题】:How can I run many C++ source files in one CLion project?如何在一个 CLion 项目中运行多个 C++ 源文件?
【发布时间】:2021-09-06 19:04:09
【问题描述】:

我使用 CLion 作为 IDE。当我创建一个项目时,会自动添加一个 main.cpp。我想在一个项目中添加 30-40 个 cpp 文件并将它们保留为一个项目。基本上,我只想在一个文件夹中创建许多 .cpp 文件并让 CLion 运行它们。我可以在 Pycharm 中做到这一点,只需创建一个项目并添加任意数量的 .py 文件即可。但是当我想在 CLion 上执行此操作时,我遇到了一个错误。是否可以在 CLion 的项目中添加许多 .cpp 文件,如果可以,我该怎么做? 在下面可以看到一个错误。我在项目中添加了一个 second.cpp 并运行,出现此错误消息。

=====================[构建|试用 |调试]=====================================

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/mertsaner/CLionProjects/trial/cmake-build-debug --target trial -- -j 6
Scanning dependencies of target trial
[ 66%] Building CXX object CMakeFiles/trial.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/trial.dir/second.cpp.o
[100%] Linking CXX executable trial
duplicate symbol '_main' in:
    CMakeFiles/trial.dir/main.cpp.o
    CMakeFiles/trial.dir/second.cpp.o
ld: 1 duplicate symbol for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [trial] Error 1
make[2]: *** [CMakeFiles/trial.dir/all] Error 2
make[1]: *** [CMakeFiles/trial.dir/rule] Error 2
make: *** [trial] Error 2

【问题讨论】:

  • Python 和 C++ 语言是完全不同的语言。 C++ 有不同的构建系统,Clion 使用 CMake,所以你必须看看如何使用 Cmake,以满足自己的需要。
  • 似乎你有 2 个来自 CMake 日志的主要功能。
  • @是的,每个cpp文件都有自己的main函数。我想运行它们,就像它们是单独的项目一样,或者就像我在命令行上运行它们一样
  • 这就是日志所说的,在 C++ 中你只能有一个 main 函数。
  • 我个人没有使用过 Clion,也没有使用过 CMake。但为此我真的会使用更简单的 VSCode。

标签: c++ clion


【解决方案1】:

Clion 使用 Cmake。如果您想在一个目录中创建多个可执行文件,例如名称为(ex1.cpp、ex2.cpp、ex3.cpp),您将在您目录的 CMake 文件中执行类似的操作。

cmake_minimum_required(VERSION 3.18)
project(some_project)
set(CMAKE_CXX_STANDARD 20)
add_executable(executable1 ex1.cpp)
add_executable(executable2 ex2.cpp)
add_executable(executable3 ex3.cpp)

等等..

【讨论】:

  • 这正是我想做的。非常感谢!现在我可以选择我想使用的任何可执行文件,并且可以在一个目录中创建任意数量的 .cpp 文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-15
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多