c++ - 使用外部文件和链接文件

标签 c++

所以我应该链接这 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/

相关文章:

c++ - 不同类型的指针之间的分配有效,我不知道为什么

c++ - 如何解析 KLV 数据?

c++ - OpenMP - 执行期间挂起

C++ 陷入循环

c++ - 通过单次迭代删除所有逗号、点和小写字符串

C++ Eclipse Galileo 让它显示行号 - 怎么样?

c++ - QT c++ 画点 qpainter qgraphicsscene

c++ - gcc是否将非常量表达式函数的内置函数视为常量表达式

c++ - shared_ptr with = symbol not allowed

c++ 条件单向迭代器