gpt4 book ai didi

c++ - .h 文件有问题,它似乎没有正确链接

转载 作者:搜寻专家 更新时间:2023-10-31 02:09:50 25 4
gpt4 key购买 nike

<分区>

我正在通过 Bjarne Stroustrup 的“使用 C++ 的编程原理和实践”学习 C++,本章练习的第一部分是展示 .h 文件的使用。练习非常简单,我严格按照它进行操作,但是当我尝试编译时,我不断收到“对 foo 的 undefined reference ”。我会把这三个文件写在下面,它们非常短。

顺便说一下,std_lib_facilities.h 是作者包含的一个 .h 文件,其中包含一些像 iostream 以及“using namespace std;”以及作者写的一些你不时需要用到的函数

my.h - 包含 extern int foo; void print_foo();无效打印(int);

#ifndef MY_H
#define MY_H

extern int foo;
void print_foo();
void print(int);

#endif

my.cpp - 包含 my.h,定义 print_foo() 以使用 cout 打印 foo 的值,并定义 print(int i) 以使用 cout 打印 i 的值

#include "my.h"
#include "std_lib_facilities.h"

void print_foo()
{
cout << foo << '\n';
}

void print(int i)
{
cout << i << '\n';
}

use.cpp - includes my.h defines main() to set the value of foo to 7 and print it using print_foo() and print the value of 99 using print()

#include "my.h"

int main()
{
foo = 7;
print_foo();
print(99);
}

如果有帮助,我为此使用的操作系统是 Linux

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com