C++ 如何从多个cpp文件调用函数

C++ 如何从多个cpp文件调用函数,c++,lnk2019,C++,Lnk2019,如何在多个cpp文件中包含和使用该函数 // my_function.h void func_i_want_to_include(); // my_function.cpp void func_i_want_to_include() { puts("hello world"); } //main.cpp #include "my_function.h" #include "file_b.h" int main() { call_to_file_b(); fun

如何在多个cpp文件中包含和使用该函数

// my_function.h
void func_i_want_to_include();

// my_function.cpp
void func_i_want_to_include()
{
    puts("hello world");
}


//main.cpp
#include "my_function.h"
#include "file_b.h"

int main()
{
     call_to_file_b();
     func_i_want_to_include();
     return 0;
}

//file_b.h
void call_to_file_b();

//file_b.cpp
#include "my_function.h"

void call_to_file_b()
{
     puts("from file b\n");
     func_i_want_to_include();
}
当我这样做时,我通过链接器“unsolve external symbol”得到了收益,我猜链接器经过
func\u我希望\u包含()
2次,而不是理解这是同一个函数


我如何告诉他‘这是同一个函数?只需从两个文件调用它,但不要尝试制作同一函数的两个副本?

如果要将函数定义放入头文件,则需要将函数内联

// my_function.h
inline void func_i_want_to_include()
{
    puts("hello world");
}
否则,编译器将为包含头文件的每个cpp文件创建一个函数,链接器将不知道选择哪个函数

这就是为什么您通常将函数声明分开

void func_i_want_to_include();
从函数定义

void func_i_want_to_include()
{
    puts("hello world");
}

前者进入头文件,而后者进入源文件。

您必须创建头文件。在该文件中创建一个对象并在类中实现一个公共函数。

正如eozd所说,头文件用于声明。但您的问题是头文件被包含了好几次,每个#include子句包含一次。 你可以解决这个问题

#pragma once
在页眉顶部,或以旧的方式:

#ifndef _MY_HEADER_FILE_H_
#define _MY_HEADER_FILE_H_

// Stuff

#endif // _MY_HEADER_FILE_H_

首先,正如@LuisGP所提到的,如果头文件多次被包含,则需要#ifndef。其次,在关联的cpp文件中,需要包含头文件。头文件声明函数,cpp文件描述函数。最后,编译时必须包含所有cpp文件(如果编辑器不工作,只需使用命令行)。事情是这样的:

gcc -c my_function.cpp file_b.cpp //this will make a my_function.o and a file_b.o file
gcc -o main main.cpp my_function.o file_b.o 
简言之:

gcc -o main main.cpp my_function.cpp file_b.cpp
以下是应如何写入文件:

// my_function.h
#ifndef _my_function_h
#define _my_function_h

void func_i_want_to_include(); 

#endif


// my_function.cpp
#include <stdio.h>
#include "my_function.h"

void func_i_want_to_include()
{
    puts("hello world");
}


//file_b.h
#ifndef _file_b_h
#define _file_b_h

void call_to_file_b();

#endif


//file_b.cpp
#include <stdio.h>
#include "my_function.h"
#include "file_b.h"

void call_to_file_b()
{
     puts("from file b\n");
     func_i_want_to_include();
}


//main.cpp
#include "my_function.h"
#include "file_b.h"

int main()
{
     call_to_file_b();
     func_i_want_to_include();
     return 0;
}
//my_function.h
#如果没有我的功能
#定义我的函数
void func_i_希望包含();
#恩迪夫
//my_function.cpp
#包括
#包括“my_function.h”
void func\u我希望包含()
{
puts(“你好世界”);
}
//文件_b.h
#ifndef文件
#定义文件
无效调用_到_文件_b();
#恩迪夫
//文件_b.cpp
#包括
#包括“my_function.h”
#包括“文件_b.h”
无效调用\u到\u文件\u b()
{
放入(“从文件b\n”);
func_i_希望包含();
}
//main.cpp
#包括“my_function.h”
#包括“文件_b.h”
int main()
{
调用_到_文件_b();
func_i_希望包含();
返回0;
}

这是我第一次回答问题,如果我犯了任何错误,告诉我,我会改正的。希望有帮助。

头文件用于声明函数。您需要在一个单独的cpp文件中提供
func\u i\u want\u include
的实现。(除非标题中的函数是模板函数)“unsolve external symbol”表示找不到符号。
main.cpp
如何知道
call_to_file_b()
?@eozd这是不正确的。它们是按约定分开的。没有理由头不能包含实现。我尝试将实现分离为.cpp,但仍然得到相同的结果。您是如何编译代码的?分离没有帮助。我尝试在visual studio 2015 x86版本中编译。您必须提供更完整的信息,因为安装中肯定还有其他问题。使用内联功能对您有用吗?您是否正在与其他cpp文件一起构建my_function.cpp?my_function.cpp也包括my_function.h?是的,我一起编译,my_function.cpp包括my_function.h。这里没有我可以使用的函数的extern关键字?extern用于全局变量。你能用完整的包含文件更新这个例子吗?你在用什么编译器。我很确定链接器无法将您的my_function.cpp与程序的其余部分联系起来。这是最完整的答案,包括所有注释和以前的部分答案。应标记为正确答案。