今天在学习c++时,遇到一个问题,如下,记录一下解决方法:
场景: 通过一个程序引入另一个程序的头文件,从而实现调用另一个文件中的函数。
原因:创建程序文件是没有将其添加到构建项目中,导致程序运行时无法加载相应文件。
解决方式:在已创建的程序文件是点击右键–Properties–build
选中Debug与Release,点击ok即可。
代码如下:
support.cpp
#include <stdio.h>
#include "support.h"
int add(int a, int b){
return a + b;
}
support.h
#ifndef SUPPORT_H_INCLUDED
#define SUPPORT_H_INCLUDED
int add(int a, int b);
#endif
main.cpp
#include <iostream>
#include <stdio.h>
#include "support.h"
int main()
{
int a = 1, b = 2;
int c = add(a,b);
printf("c=%d", c);
return 0;
}