Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36734
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 258
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-08 15:12
文章分类

全部博文(25)

文章存档

2015年(2)

2014年(23)

我的朋友

分类: C/C++

2014-02-09 18:14:07

main.cpp

点击(此处)折叠或打开

  1. #include <iostream>
  2. #include "add.h" // this brings in the declaration for add()
  3.   
  4. int main()
  5. {
  6.    using namespace std;
  7.    cout << "The sum of 3 and 4 is " << add(3,4) << endl;
  8.    return 0;
  9. }

add.cpp

点击(此处)折叠或打开

  1. int add(int x,int y)
  2. {
  3.    return x+y;
  4. }

add.h

点击(此处)折叠或打开

  1. #ifndef ADD_H
  2. #define ADD_H

  3. int add(int x,int y); //function prototype for add.h

  4. #endif

下Microsoft Visual C++ 2010
IDE下编译自动识别,编译不会出错
在Linux下
直接编译main.cpp是会报

 g++ -o main main.cpp
/tmp/ccVwA0IL.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `add(int, int)'
collect2: ld 返回 1

解决方法
g++ -o main add.cpp main.cpp
编译通过


阅读(987) | 评论(2) | 转发(0) |
0

上一篇:C++学习笔记

下一篇:Linux常用命令

给主人留下些什么吧!~~

zhangman11232014-03-05 17:31:28

jie2515:#include <iostream>
//#include "add.h"

int main()

 int add(int x, int y);
 using namespace std;
 cout << "The sum of 3 and 4 is " << add (3,4) << endl;
 return 0;

int add (int x, int y){return x+y;}
不要头文件也可以编译运行,不会是把头文件写错了吧

你那源码在同一个文件里面吧。 如果没有加 include "add.h" 会报错的

回复 | 举报

jie25152014-02-16 00:19:09

#include <iostream>
//#include "add.h"

int main()

 int add(int x, int y);
 using namespace std;
 cout << "The sum of 3 and 4 is " << add (3,4) << endl;
 return 0;

int add (int x, int y){return x+y;}
不要头文件也可以编译运行,不会是把头文件写错了吧