所以我应该链接这 3 个文件,但是当我运行 use.cpp 时它告诉我 print_foo 和 print 有问题所以我的链接或声明一定有问题;(仅供引用,我正在使用 Xcode 来编译)
这是头文件
// my.h (HEADER FILE)
extern int foo;
void print_foo();
void print(int);
这是我的.cpp
// my.cpp
#include "my.h"
#include "std_lib_facilities_5.h"
void print_foo() {
cout << foo;
}
void print(int i) {
cout << i;
}
int main() {
return 0;
}
这是use.cpp
// use.cpp
#include "my.h" /* Declaration made available here */
int foo;
int main() {
foo = 7;
print_foo();
print(99);
return 0;
}
最佳答案
你没有提到你是如何编译它们的,你可以在类似 unix 的系统上编译它们
$ gcc my.cpp use.cpp -o my
编译
关于c++ - 使用外部文件和链接文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46458224/