C / C ++エンジニアリングプロジェクト開発仕様

C / C ++エンジニアリングプロジェクト開発仕様

関数の宣言と定義

宣言:繰り返し宣言できる関数があることをシステムに伝えます。

定義:関数の実装方法であり、繰り返すことはできません。

そのエラーの場所:

ソースプログラム—プリコンパイル済み-------> ***コンパイル済み(未宣言)*** —生成されたオブジェクトファイル。o--------->リンク(未定義) —> exeファイル

  • 関数の宣言と定義を一緒に書く
#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 ;
}

最初に2つのソースファイルをコンパイルしてから、2つのオブジェクトファイルをリンクします。

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

ヘッダーファイルとソースファイル

g +±Etest.hインクルードのインポートされたコンテンツを表示します。

条件付きの定義。ヘッダーファイルが定義されていない場合は、ヘッダーを定義します。一度コンパイルして繰り返すという問題を解決します。

#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

開発仕様と静的リンクライブラリ

プロジェクトファイル内

  • ヘッダーファイルをインクルードします。h

  • srcソースfile.cc +オブジェクトfile.o

  • lib静的リンクライブラリ。一連のソースファイルによって生成されたオブジェクトファイルの.libパッケージ

    パッケージングコマンド:ar-r libXXXX.a header1.o header2.o header.o

  • xxx.cpp

  • makefile

include <XXX.h>。システムライブラリパスでXXX.hを検索します。

「XXX.h」を含めます。現在のフォルダでXXX.hを見つけます

「」を<>に変更する場合は、システムライブラリパスにインクルードを追加する必要があります。

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