链接两个.cpp和一个.h文件 [英] Linking two .cpp and a .h files

查看:220
本文介绍了链接两个.cpp和一个.h文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个练习(从C ++的思维的第三章),但是我有一个问题链接两个.cpp文件。



这是练习:


创建头文件(扩展名为'.h')。在此文件中,
通过更改参数列表
声明一组函数,并返回以下值之一:void,char,
int和float。现在创建一个包含
头文件的.cpp文件并为所有这些
函数创建定义。每个定义应该只打印出
函数名,参数列表和返回类型,所以你
知道它被调用。创建第二个.cpp文件,
包含头文件并定义int main(),
包含对所有函数的调用。编译并运行
程序。


我创建了三个文件,但是当我尝试编译编译器给我这个错误:


未定义引用`func1()'



未定义引用`func2 / p>

未定义引用`func3(char,int,double)'|



'|



|| === Build finished:4 errors,0 warnings === |


为什么找不到函数声明?



## EDIT

我的三个文件:



func_ex.h

  void func1(void); 
int func2(int i);
char func3(char c,int i,double d);
float func4(void);

func_ex.cpp

  #includefunc_ex.h
using namespace std;

void func1(void){
cout< void func1(void)< endl;
}

int func2(int i){
cout< int func2(int i)< endl;
}

char func3(char c,int i,double d){
cout< func3(char c,int i,double d)< endl;
}

float func4(void){
cout< func4(void)< endl;
}

func_ex_main.cpp

  #includefunc_ex.h
#include< iostream>
using namespace std;

int main(int argc,char * argv []){
func1();
int i;
func2(i);
char c;双d;
func3(c,i,d);
func4();
}



我是usig GCC编译器(和Code :: Blocks as IDE,但我认为这不重要)。

解决方案

这听起来像文件没有找到适当的功能。两个文件都包含头文件吗?您可以包括:

  #includemyheader.h
pre>

您确定要将这两个文件编译在一起吗?例如:



gcc -o myprogram file1.cpp file2.cpp


I'm doing an exercise (from the third chapter of Thinking in C++) but I have a problem linking two .cpp files.

This is the exercise:

Create a header file (with an extension of ‘.h’). In this file, declare a group of functions by varying the argument lists and return values from among the following: void, char, int, and float. Now create a .cpp file that includes your header file and creates definitions for all of these functions. Each definition should simply print out the function name, argument list, and return type so you know it’s been called. Create a second .cpp file that includes your header file and defines int main( ), containing calls to all of your functions. Compile and run your program.

I've created the three files, but when I try to compile the compiler give me this error:

undefined reference to `func1()'

undefined reference to `func2(int)'|

undefined reference to `func3(char, int, double)'|

undefined reference to `func4()'|

||=== Build finished: 4 errors, 0 warnings ===|

Why it cannot found the function declaration?

##EDIT
These are my three files:

func_ex.h

void func1(void);
int func2(int i);
char func3(char c, int i, double d);
float func4(void);

func_ex.cpp

#include "func_ex.h"
using namespace std;

void func1(void) {
    cout << "void func1(void)" << endl;
}

int func2(int i) {
    cout << "int func2(int i)" << endl;
}

char func3(char c, int i, double d) {
    cout << "func3(char c, int i, double d)" << endl;
}

float func4(void) {
    cout << "func4(void)" << endl;
}

func_ex_main.cpp

#include "func_ex.h"
#include <iostream>
using namespace std;

int main(int argc, char* argv[]) {
    func1();
    int i;
    func2(i);
    char c; double d;
    func3(c, i, d);
    func4();
}

I'm usig GCC as compiler (and Code::Blocks as IDE, but I think that's not important).

解决方案

It sounds like the file is not finding the functions appropriately. Is the header file included in both files? You can include it like:

#include "myheader.h"

Did you make sure to compile both files together? Such as:

gcc -o myprogram file1.cpp file2.cpp

这篇关于链接两个.cpp和一个.h文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆