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是大写的

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

make && ./a.out 默认make第一个内容,这里是“all”

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

make compile && ./a.out 指定需要运行的内容名字,这里是“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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 7,858评论 0 3
  • Makefile规则 一句话总结就是依赖关系,简单如下所示 target … : prerequisites … ...
    明明就_c565阅读 10,227评论 0 2
  • 离我住的小区不远,有一家开了近十年的西饼店,店很小,是家分店,白天女主人在店里销售,男主人开着小送货车往各个分店送...
    虎笨笨阅读 2,523评论 0 0
  • 梧桐雨落花自好, 风景流年等闲过。 山光美图月下闲, 似水年华韶光度。
    心腹郁花自香阅读 2,934评论 0 2
  • 姓名:王薇 公司:扬州方圆建筑工程有限公司 【日精进打卡第51天】 【知~学习】 《六项精进》遍 共273遍 《大...
    b03815a7aaf5阅读 1,117评论 0 0

友情链接更多精彩内容