我有两个简单的文件:
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;
}
add.cpp
int add(int x, int y)
{
return x + y;
}
我的CMakeLists看起来像这样:
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编译器似乎看不到它。