Ошибка компиляции при сборке нескольких файлов - PullRequest
0 голосов
/ 21 февраля 2020

У меня есть два простых файла:

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

Я жду объявления о добавлении, но похоже, что его не видит компилятор CLion.

...