无法在 CLion 中构建 C++ 项目
Can't build c++ project in CLion
我有一个 hello-wrold 项目:
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
P.S. 我以前写过很长一段时间的简单的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)
add_executable
期望需要编译以创建所述可执行文件的文件列表。将 square.cpp
添加到该列表。
add_executable(cpp_code main.cpp square.cpp)
我有一个 hello-wrold 项目:
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
P.S. 我以前写过很长一段时间的简单的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)
add_executable
期望需要编译以创建所述可执行文件的文件列表。将 square.cpp
添加到该列表。
add_executable(cpp_code main.cpp square.cpp)