UbuntuでC言語プログラムをコンパイルするための3つの方法


トピック

メインプログラムファイルmain1.cとサブプログラムファイルsub1.cを記述します。要件:サブプログラムsub1.cには、算術演算関数float x2x(int a、int b)が含まれています。この関数は、2つの整数パラメーターを入力します。特定の演算を実行し、結果を浮動小数点数として返します。

  1. メインプログラムmain1.cをubuntuシステムのgccコマンドラインでコンパイルして実行します。
  2. Windowsシステムで、使い慣れたコンパイルツールを使用してメインプログラムmain1.cをコンパイルし、実行します。
  3. Makefileを使用して、ubuntuシステムでメインプログラムをプログラムします

1.必要な環境をコンパイルします

Utuntu、VC ++ 6.0

2、アプリケーション

1.Ubuntu

(1)sub1.cを書く

float x2x(int a,int b)
{
    
    
    float c;
    c=a+b;
    return c;
}

(2)main.cを書く

#include "sub1.c"
#include<stdio.h>
void main()
{
    
    
     int x=2,y=3;
     printf("%f",x2x(x,y));
}

(3)gccを使用してメインプログラムを実行します

gcc main1.c
./main1

(4)運転結果

ここに画像の説明を挿入

2.VC ++ 6.0

(1)main.cを書く

#include<stdio.h>
float x2x(int a,int b)
{
    
    
	return a+b;
}
void main()
{
    
    
	int x=2,y=3;
	printf("%f",x2x(x,y));
}

(2)運転結果

3.makefileを使用してコンパイルおよび実行します

(1)makefikeファイルを作成します

touch makefile

(2)makefikeファイルを書き込む

main: main1.o
	gcc main1.o -o main    //gcc前面应该打Tab,而不是空格
main1:main1.c
	gcc -c main1.c
clean:
	rm -f *.o main

(2)makeコマンドを実行して実行します

make
./main

(3)運転結果

ここに画像の説明を挿入


3、まとめ

UbuntuでCファイルを実行する方法はたくさんあります。gccの実行は比較的簡単ですが、makefileはファイルの内容を維持できます。

おすすめ

転載: blog.csdn.net/weixin_47357131/article/details/108768189