Create linux make/build file

前端 未结 10 1356
余生分开走
余生分开走 2020-12-13 01:01

I am moving a C++ project from Windows to Linux and I now need to create a build/make file. I have never created a build/make file before. I also need to include Boost libra

10条回答
  •  北海茫月
    2020-12-13 01:43

    First off, I am not an expert on Makefiles. I know the basics, but the Makefile in this answer will almost certainly have room for improvement. There are a lot of tricks and shortcuts that will allow you to handle dependencies and adding files better than hardcoding everything. However I think this example will be sufficient for your purposes and will possibly be more educational.

    Second, I want to make sure you know the basic stages of building a project. You may already know this, but if you don't the following will be a bit confusing at first. There are essentially two steps: compiling and linking. Compiling converts your code to object code - it will convert a .cpp file to a .o file if successful. The next step is linking. This is where it sticks all the object code together to create an executable and links the function calls from one file to another (so if file1.cpp calls a function defined in file2.cpp, it's at this step that the file1.cpp figures out where the function actually is). There is more to it but that should be enough to make the following clear.

    At this point you can just use g++ to compile and link your project (you can even do it in "one" step). This is quite cumbersome however, especially for any non-trivial project. It also makes it difficult to track files that have changed since you last compiled.

    This is where Makefiles come in. A Makefile is a list of rules in the form:

    target: dependencies
        command
    

    (Make sure to use tabs not spaces, make probably won't work if you use spaces). If you run the command:

    make some_target
    

    Then make will look for the rule with some_target. If the target is a file, it will check the 'last modified' time stamp of the file, and it will check the time stamps of all the dependencies you have listed. If any of the dependencies have a later time stamp, it will run the command.

    I'm going to have to make some assumptions about your project (namely, which files depend on which files) so you will probably have to modify the following, but here is a basic Makefile for your project (and remember, tabs not spaces. If you copy and paste this it won't work):

    CC = g++
    INCLUDE_DIRS = -I/path/to/boost
    
    all: binary_file
    
    clean:
        rm *.o
        rm binary_file
    
    binary_file: simple_ls.o rawr.o converter.o
        $(CC) -o binary_file simple_ls.o rawr.o converter.o
    
    rawr.o: rawr.h rawr.cpp 2dquicksort.h
        $(CC) -c rawr.cpp $(INCLUDE_DIRS)
    
    simple_ls.o: simple_ls.h simple_ls.cpp 2dquicksort.h
        $(CC) -c simple_ls.cpp $(INC*emphasized text*LUDE_DIRS)
    

    Running make or make all or make binary_file will cause all your files to be compiled if necessary and then linked to create an executable called binary_file. There are some improvements you can make, for example:

    %.o: %.cpp %.h 2dquicksort.h
        $(CC) -c $<
    

    Which will find all the .cpp files and compile them in to .o files. The .o files will be dependent on a .cpp file and a .h file of the same name (and 2dquicksort.h).

提交回复
热议问题