首页 > 解决方案 > 链接头文件c ++时对函数的未定义引用

问题描述

我正在尝试在 Bjarne stroustrup 的书中进行第 8 章的练习。我已按照所有步骤操作,但在运行程序时出现两个错误:未定义对“print_foo”的引用未定义对“print(int)”的引用。我使用 VSC。

这是我的文件:

------我的.h-----

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

--------my.cpp--------

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

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

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

--------使用.cpp--------

#include<iostream> 
#include"my.h" 

using namespace std;

inline void keep_window_open() {char cc;cin>>cc;}

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

    return 0;
}

标签: c++visual-studio-code

解决方案


推荐阅读