C/C++ Engineering Project Development Specification

C/C++ Engineering Project Development Specification

Function declaration and definition

Declaration: Tell the system that there is a function that can be declared repeatedly.

Definition: How the function is implemented, and cannot be repeated.

Its error location:

Source program —precompiled------->***compiled (undeclared)***—generated object file. o---------> link (undefined) —>executable file

  • Write function declaration and definition together
#include<stdio.h>

int add(int a, int b) {
    
    
    return a+b;
}

int main() {
    
    
    add(2, 3);
    return 0;
}

The function called must be defined before.

#include<stdio.h>
void fundB(int n) {
    
    
    if (n == 0) return ;
    printf("fundB: %d\n", n);
    return ;
}

void fundA(int n) {
    
    
    if (n == 0) return ;
    printf("fundA: %d\n", n);
    fundB(n - 1);
    return ;
}

int main() {
    
    
    fundA(5);
    return 0;
}
  • Declare at the front, define at the back
#include<stdio.h>
void fundA(int);
void fundB(int);

void fundB(int n) {
    
    
    if (n == 0) return ;
    printf("fundB: %d\n", n);
    fundA(n -1);
    return ;
}

void fundA(int n) {
    
    
    if (n == 0) return ;
    printf("fundA: %d\n", n);
    fundB(n - 1);
    return ;
}

int main() {
    
    
    fundA(5);
    return 0;
}
g++ testAB.cpp -c  // 只编译不链接 产生对象文件即.o文件。
g++ testAB.o       //链接对象文件。

Function is defined in other functions

#include<stdio.h>

void fundA(int);
void fundB(int);

int main() {
    
    
    fundA(5);
    return 0;
}
#include<stdio.h>
void fundA(int );
void fundB(int );

void fundB(int n) {
    if (n == 0) return ;
    printf("fundB: %d\n", n);
    fundA(n -1);
    return ;
}

void fundA(int n) {
    if (n == 0) return ;
    printf("fundA: %d\n", n);
    fundB(n - 1);
    return ;
}

Compile the two source files first, and then link the two object files;

g++ -c unite.cpp
g++ -c testAB.cpp
g++ testAB.o unite.o
./a.out

Header and source files

g+±E test.h View the imported content of include.

Conditional definition. If there is no header file defined, then define the header. Solve the problem of compiling once and repeating.

#ifndef _HEADER2_H
#define _HEADER2_H

#endif

Header file: only contains the declaration of the function;

Source file: only contains the definition of the function:

Header1 header file header1.h

#ifndef _HEADER1_H
#define _HEADER1_H
void funcA(int);
void funcB(int);

#endif

Header1 source file header1.cc

#include<stdio.h>
#include"header1.h"

void funcA(int n) {
    
    
    if(n == 0) return ;
    printf("funcA: %d\n", n);
    funcB(n-1);
    return ;
}

void funcB(int n) {
    
    
    if(n == 0) return ;
    printf("funcB: %d\n", n);
    funcA(n - 1);
    return ;
}

Header2 header file header2.h

#ifndef _HEADER2_H
#define _HEADER2_H
#include<stdio.h>
#include"header1.h"
void funcC(int, int);

#endif

Header2 source file header2.cc

#include"header1.h"
#include<stdio.h>

void funcC(int a, int b) {
    
    
    printf("funcC: %d + %d = %d\n", a, b, a + b);
    funcA(a);
    return ;
}

Header3 header file header3.h

#ifndef _HEADER3_H
#define _HEADER3_H
#include"header1.h"
void funcD(int a, int b);
#endif

Header3 source file header3.cc

#include"header1.h"
#include<stdio.h>

void funcD(int a, int b) {
    printf("funcD: %d + %d = %d\n", a, b, a + b);
    funcA(a);
    return ;
}

Main program: test.cpp

#include<stdio.h>
#include"header1.h"
#include"header2.h"
#include"header3.h"


int main() {
    
    
    funcA(5);
    funcC(6, 7);
    funcD(7, 8);
    return 0;
}

Compile and link:

g++ -c test.cpp
g++ -c header1.cc
g++ -c header2.cc
g++ -c header3.cc
g++ test.o  header1.o header2.o header3.o
./a.out

Development specification and static link library

In the project file

  • include header file.h

  • src source file.cc + object file.o

  • lib static link library. a .lib package of object files generated by a set of source files

    Packaging command:ar-r libXXXX.a header1.o header2.o header.o

  • xxx.cpp

  • makefile

include<XXX.h>. Find XXX.h in the system library path

include "XXX.h". Find XXX.h in the current folder

If you change "" to <>, you need to add include to the system library path:

g++ -I /include -c src/header1. cc

The link command of xxx.o of xxx.cpp and static link library:g++ test.o -L./lib -ltest

makeflie tool

.PHONY:clean
all: lib/libtest.a test.o
	g++ test.o -L./lib -ltest
test.o: test.cpp
	g++ -I ./include/ -c test.cpp
clean:
	rm -rf a.out test.o

Guess you like

Origin blog.csdn.net/weixin_40414160/article/details/114801695
Recommended