I have a hello-wrold project:
main.cpp:
#include <iostream>
#include "square.h"
int main() {
int z = 5;
std::cout << square(z) << std::endl;
return 0;
}
square.cpp:
int square(int x)
{
return x * x;
}
square.h:
#ifndef SQUARE_H
#define SQUARE_H
int square(int x);
#endif //SQUARE_H
该代码可以在终端中使用g++编译并运行:
g++ -c main.cpp
g++ -c square.cpp
g++ main.o square.o -o programm
./programm
>25
但是CLion的构建最终出现了以下问题:
[ 50%] Linking CXX executable cpp_code
Undefined symbols for architecture x86_64:
"square(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[3]: *** [cpp_code] Error 1
make[2]: *** [CMakeFiles/cpp_code.dir/all] Error 2
make[1]: *** [CMakeFiles/cpp_code.dir/rule] Error 2
make: *** [cpp_code] Error 2
附言:我曾经长时间(虽然有很长的间歇期)编写简单的 C++ 程序,这是我第一次在这个步骤遇到问题。此外,有一个 CMakeList.txt
文件是自动生成的(在 VisStud 中没有类似的东西!)
CMakeList.txt:
cmake_minimum_required(VERSION 3.12)
project(cpp_code)
set(CMAKE_CXX_STANDARD 14)
add_executable(cpp_code main.cpp)
square.cpp
。 - Some programmer dude