CPP project / makefile

files in the folder:

ls -l
-rw-r--r-- 1 ysgc staff 98 Aug 15 13:40 func1.cpp
-rw-r--r-- 1 ysgc staff 106 Aug 15 13:30 func2.cpp
-rw-r--r-- 1 ysgc staff 41 Aug 15 12:45 funcs.h
-rw-r--r-- 1 ysgc staff 198 Aug 15 13:48 main.cpp

// main.cpp
#include <iostream>
#include "funcs.h"
int main(){
    std::cout << std::endl;
    print_hello();
    std::cout << std::endl;
    std::cout << "The factorial of 5 is " << factorial(5) << std::endl;
    return 0;
}
//funcs.h
void print_hello();
int factorial(int n);
// func1.cpp
#include "funcs.h"

int factorial(int n){
    if (n != 1){
        return n*factorial(n-1);
    }
    return 1;
}
// func2.cpp
#include <iostream>
#include "funcs.h"

void print_hello(){
    std::cout << "hello world!!!" << std::endl;
}

1. Brute force

clang++ main.cpp func1.cpp func2.cpp -o hello && ./hello

hello world!!!

The factorial of 5 is 120

2. Makefile example 1

https://www.youtube.com/watch?v=aw9wHbFTnAQ

subl Makefile 注意這里M是大寫(xiě)的

all:
    clang++ main.cpp func1.cpp func2.cpp -o a.out
compile:
    

make && ./a.out 默認(rèn)make第一個(gè)內(nèi)容,這里是“all”

all:
    
compile:
    clang++ main.cpp func1.cpp func2.cpp -o a.out

make compile && ./a.out 指定需要運(yùn)行的內(nèi)容名字,這里是“compile”

all: main.o func1.o func2.o # has these dependencies
    clang++ main.o func1.o func2.o -o a.out && ./a.out

main.o: main.cpp # check if main.cpp exists or not
    clang++ -c main.cpp

func1.o: func1.cpp
    clang++ -c func1.cpp

func2.o: func2.cpp
    clang++ -c func2.cpp

clean:
    rm -rf *o a.out

# format
# target: dependency
#   command

make && make clean

# define variables
CC=clang++
CFLAGS=-c -Wall

all: main.cpp func1.o func2.o # has these dependencies
    $(CC) main.cpp func1.o func2.o -o a.out && ./a.out

func1.o: func1.cpp # check if func1.cpp exists or not
    $(CC) $(CFLAGS) func1.cpp

func2.o: func2.cpp
    $(CC) $(CFLAGS) func2.cpp

clean:
    rm -rf *o a.out

# format
# target: dependency
#   command

make && make clean

3. Makefile example 2

https://www.youtube.com/watch?v=_r7i5X0rXJk

// main.cpp
#include <cstdlib>
#include "message.h"

using namespace std;

int main(){
    message m;
    m.printMessage();

    return 0;
}
//message.h
#ifndef MESSAGE_H
#define MESSAGE_H

class message{
public:
    void printMessage();
};

#endif
//message.cpp
#include <iostream>
#include "message.h"
using namespace std;

void message::printMessage(){
    cout << "Makefile example\n";
}
#Makefile
all: main.cpp message.o
    clang++ main.cpp message.o -o a.out && ./a.out

message.o: message.cpp
    clang++ -c message.cpp

clean:
    rm -rf *o a.out

4. Example 3

https://www.youtube.com/watch?v=j02hcX7R1yI

  • directly output an executable file
    • -o a.out, -o exe_file
  • two steps:
    • link file: -c file1.cpp or -c file1.cpp -o file1.o
    • to exe: xxx.o yyy.o zzz.o -o exe_file
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,203評(píng)論 0 3
  • Makefile規(guī)則 一句話總結(jié)就是依賴(lài)關(guān)系,簡(jiǎn)單如下所示 target … : prerequisites … ...
    明明就_c565閱讀 4,875評(píng)論 0 2
  • 離我住的小區(qū)不遠(yuǎn),有一家開(kāi)了近十年的西餅店,店很小,是家分店,白天女主人在店里銷(xiāo)售,男主人開(kāi)著小送貨車(chē)往各個(gè)分店送...
    虎笨笨閱讀 356評(píng)論 0 0
  • 梧桐雨落花自好, 風(fēng)景流年等閑過(guò)。 山光美圖月下閑, 似水年華韶光度。
    心腹郁花自香閱讀 428評(píng)論 0 2
  • 姓名:王薇 公司:揚(yáng)州方圓建筑工程有限公司 【日精進(jìn)打卡第51天】 【知~學(xué)習(xí)】 《六項(xiàng)精進(jìn)》遍 共273遍 《大...
    b03815a7aaf5閱讀 130評(píng)論 0 0

友情鏈接更多精彩內(nèi)容