C/C++工程项目开发规范

C/C++工程项目开发规范

函数的声明与定义

声明:告诉系统有一个函数,可以重复声明。

定义:函数具体是如何实现的,不可以重复。

其报错位置:

源程序—预编译------->***编译(未声明)***—生成对象问件.o---------> 链接(未定义) —>可执行文件

  • 将函数的声明与定义写在一起
#include<stdio.h>

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

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

所调用的函数必须在之前定义。

#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;
}
  • 声明在前面,定义在后
#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       //链接对象文件。

函数定义在其它函数中

#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 ;
}

先编译两个源文件,后链接两个目标文件;

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

头文件与源文件

g+±E test.h 查看include导入的内容。

条件式 定义。如果没有定义头文件,则定义头。 解决一次编译,重复问题。

#ifndef _HEADER2_H
#define _HEADER2_H

#endif

头文件:只含函数的声明;

源文件:只包含函数的定义:

Header1 头文件 header1.h

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

#endif

Header1 源文件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 头文件 header2.h

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

#endif

Header2 源文件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 头文件 header3.h

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

Header3 源文件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 ;
}

主程序: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;
}

编译与链接:

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

开发规范与静态链接库

项目文件中有

  • include 头文件 .h

  • src 源文件 .cc + 对象文件.o

  • lib 静态链接库 .a .lib 一组源文件生成的对象文件的打包

    打包命令:ar-r libXXXX.a header1.o header2.o header.o

  • xxx.cpp

  • makefile

include<XXX.h>. 在系统库路径中查找 XXX.h

include"XXX.h". 在当前文件夹中查找 XXX.h

若将“” 改为<> 则需要将include 添加到系统库路径中:

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

xxx.cpp 的xxx.o与静态链接库的链接命令:g++ test.o -L./lib -ltest

makeflie工具

.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

猜你喜欢

转载自blog.csdn.net/weixin_40414160/article/details/114801695