如何使用单个makefile来使makefile编译不同目录中的多个.cpp文件? [英] How to do makefile to compile multiple .cpp files in different directories using a single makefile?

查看:926
本文介绍了如何使用单个makefile来使makefile编译不同目录中的多个.cpp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的proj2目录中包含以下文件,需要将它们编译在一起以具有一个可执行文件.

I have the following files in my proj2 directories and need to compile them together to have one executable file.

    proj2/main.cpp
    proj2/model/Player.cpp
    proj2/model/gameBoard.cpp
    proj2/controller/TTTController.cpp
    proj2/Makefile

我在我的Makefile中使用以下命令,但是它不起作用.

I'm using the following command inside my makefile, but it is not working.

all:
    g++ /project2_p1/main.cpp /project2_p1/controller/TTTController.cpp          /model/gameBoard.cpp /model/Player.cpp -o ttt
clean:
    -rm ttt

有人可以帮我吗,谢谢

推荐答案

这就是我的minGW项目的Makefile代码:

thats my minGW project's makefile codes:

hepsi: derle calistir
Nesneler :=  ./lib/Hata.o ./lib/Hatalar.o ./lib/Dugum.o ./lib/ListeGezici.o ./lib/BagilListe.o

    derle:
        g++ -I ./include/ -o ./lib/Hata.o -c ./src/Hata.cpp
        g++ -I ./include/ -o ./lib/Hatalar.o -c ./src/Hatalar.cpp
        g++ -I ./include/ -o ./lib/Dugum.o -c ./src/Dugum.cpp
        g++ -I ./include/ -o ./lib/ListeGezici.o -c ./src/ListeGezici.cpp
        g++ -I ./include/ -o ./lib/BagilListe.o -c ./src/BagilListe.cpp
        g++ -I ./include/ -o ./bin/test $(Nesneler) ./src/test.cpp

    calistir:
        ./bin/test

在您的项目中,我认为这会起作用;

In your project I think this will work;

    all: compile run
Objects :=  ./lib/Player.o ./lib/gameBoard.o ./lib/TTTController.o 

compile:
    g++ -I ./include/ -o ./lib/Player.o -c ./model/Player.cpp
    g++ -I ./include/ -o ./lib/gameBoard.o -c ./model/gameBoard.cpp
    g++ -I ./include/ -o ./lib/TTTController.o -c .controller/TTTController.cpp
    g++ -I ./include/ -o ./bin/main $(Objects) ./main.cpp

run:
    ./bin/main

lib文件夹包含.o文件.如果您愿意,可以碰巧. include文件夹引用您的头文件.h.hpp文件.您可以根据标题位置更改其中的每一个.

lib folder contains .o files. You can chance it if you want. include folder refers your header .h or .hpp files. You can change every one of them according to your headers location.

bin文件夹包含名为main.exe.exe文件.您可以像这样更改或删除它

bin folder contains your .exe file called main.exe. You can change or remove it like that

run: 
   ./main

我希望它能工作.

@Galik正确.如果您想学习C++,则一定要学习make.

@Galik has right. if you want to learn C++, you should definitely learn make.

这篇关于如何使用单个makefile来使makefile编译不同目录中的多个.cpp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆