Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 生成多个文件时发生编译错误_C++_Cmake_Clion - Fatal编程技术网

C++ 生成多个文件时发生编译错误

C++ 生成多个文件时发生编译错误,c++,cmake,clion,C++,Cmake,Clion,我有两个简单的文件: main.cpp #include <iostream> int add(int x, int y); // needed so main.cpp knows that add() is a function declared elsewhere int main() { std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n'; return

我有两个简单的文件:

main.cpp

#include <iostream>

int add(int x, int y); // needed so main.cpp knows that add() is a function declared elsewhere

int main()
{
    std::cout << "The sum of 3 and 4 is: " << add(3, 4) << '\n';
    return 0;
}
我的CmakeList如下所示:

cmake_minimum_required(VERSION 3.13)
project(untitled)

set(CMAKE_CXX_STANDARD 17)

add_executable(add add.cpp)

add_executable(main main.cpp)
我得到一个错误:

====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/hectoresteban/CLionProjects/untitled/cmake-build-debug --target all -- -j 4
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[ 50%] Linking CXX executable add
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [add] Error 1
make[1]: *** [CMakeFiles/add.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 75%] Linking CXX executable main
Undefined symbols for architecture x86_64:
  "add(int, int)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

我正在向前声明add,但它似乎没有被CLion编译器看到。

add\u可执行文件(add add.cpp)add\u可执行文件(main main.cpp)
看起来不正确。你应该把
add.cpp
main.cpp
编译成一个可执行文件。你说得对,谢谢你
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/hectoresteban/CLionProjects/untitled/cmake-build-debug --target all -- -j 4
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[ 50%] Linking CXX executable add
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [add] Error 1
make[1]: *** [CMakeFiles/add.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
[ 75%] Linking CXX executable main
Undefined symbols for architecture x86_64:
  "add(int, int)", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2