初學C,用Clion遇到的問題
用了clion建了項目後
想構建一個外部函數,在main.c中引用,外部函數的例子如下:
tool.h文件如下:
int Myadd(int a, int b);
tool.c文件如下:
int Myadd(int a ,int b){
return a+b;
}
教程中,在main文件中只需要添加 #include "tool.h" 這一行代碼即可調用Myadd函數。但是我再Clion中運行後遇到了下面的錯誤。一臉懵逼。
Undefined symbols for architecture x86_64:
"_Myadd", referenced from:
_main in main.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
查了好多stackflow ,終於發現需要在main文件中再加上 #include "tool.c"。
最終順利執行。
main.c的完整代碼如下
#include <stdio.h>
#include "tool.h"
#include "tool.c"
int main() {
printf("%d\n",Myadd(2,3));
return 0;
}