Home > Backend Development > C++ > body text

How to call functions in different modules in C++?

WBOY
Release: 2024-04-12 15:54:02
Original
1074 people have browsed it

Call functions across modules in C: Declare the function: Declare the function to be called in the header file of the target module. Implement function: Implement the function in the source file. Linking modules: Use a linker to link together modules containing function declarations and implementations. Call the function: Include the header file of the target module in the module that needs to be called, and then call the function.

C++ 中如何在不同模块中调用函数?

Call functions across modules in C

In C, when the project scale increases, organize the code into different Modules improve maintainability and reusability. Modules also allow functions and variables to be shared between different modules. This tutorial will introduce how to call functions across modules and provide a practical case.

Header files and source files

Each module consists of two files: a header file and a source file. Header files contain declarations of functions and variables, while source files contain their implementations.

Function declaration

To make a function in one module callable from another module, the function must be declared in the header file. A function declaration specifies the function's return type, name, parameters, and return type. For example:

// moduleA.h
int sum(int a, int b);
Copy after login

Function implementation

In the source file, implement the function. The source file contains the actual code of the function. For example:

// moduleA.cpp
int sum(int a, int b) {
  return a + b;
}
Copy after login

Linking modules

In order for the compiler to know where the function declaration and implementation are, the modules need to be linked together. This can be done using a linker, which combines the object files of different modules into a single executable file. In the command line, use the following command to link the module:

g++ -o executable moduleA.o moduleB.o
Copy after login
Copy after login

Practical case

Suppose we have two modules: moduleA and moduleB. There is a function named greet() in moduleA, and a function named print() in moduleB. We hope to be able to call the greet() function in moduleA from moduleB.

Declare greet() in moduleA.h Function:

// moduleA.h
void greet(string name);
Copy after login

Implemented in moduleA.cppgreet() Function:

// moduleA.cpp
void greet(string name) {
  cout << "Hello, " << name << "!" << endl;
}
Copy after login

In moduleB.cpp, use the header file moduleA.h and call greet() Function:

// moduleB.cpp
#include "moduleA.h"

void print() {
  greet("John");
}
Copy after login

Link two modules:

g++ -o executable moduleA.o moduleB.o
Copy after login
Copy after login

Run the executable:

./executable
Copy after login

Output:

Hello, John!
Copy after login

The above is the detailed content of How to call functions in different modules in C++?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template