一共就这3个文件
head.h 文件内容如下
#ifndef A
#define A
// 声明函数原型
void sum1(int a,int b);
#endif //A
head.cpp 文件内容如下
#include "iostream"
//引入自定义头文件
#include "head.h"
// 函数实现
void sum1(int a,int b){
std::cout << "我是sum1" << std::endl;
}
lesson_17_head_file_main.cpp 是启动函数main的文件,内容如下
#include "head.h"
int main() {
sum1(1,2);
return 0;
}
启动后报这个错误,是为啥呀?明明已经实现了函数
原文如下
====================[ Build | lesson_17_header_file-lesson_17_head_file_main | Debug ]====
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/yexindong/Documents/project_cpp/cpp_learn/cmake-build-debug --target lesson_17_header_file-lesson_17_head_file_main -- -j 6
Consolidate compiler generated dependencies of target lesson_17_header_file-lesson_17_head_file_main
[ 50%] Building CXX object CMakeFiles/lesson_17_header_file-lesson_17_head_file_main.dir/lesson_17_header_file/lesson_17_head_file_main.cpp.o
[100%] Linking CXX executable lesson_17_header_file-lesson_17_head_file_main
Undefined symbols for architecture x86_64:
"sum1(int, int)", referenced from:
_main in lesson_17_head_file_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]: *** [lesson_17_header_file-lesson_17_head_file_main] Error 1
make[2]: *** [CMakeFiles/lesson_17_header_file-lesson_17_head_file_main.dir/all] Error 2
make[1]: *** [CMakeFiles/lesson_17_header_file-lesson_17_head_file_main.dir/rule] Error 2
make: *** [lesson_17_header_file-lesson_17_head_file_main] Error 2