Create linux make/build file

前端 未结 10 1371
余生分开走
余生分开走 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:37

    Of course you should Read The Fine Manual (specifically gcc and make). However, here are the basics for gcc:

    To compile a source file:

    g++ -c file.cpp
    

    This will create file.o. Then you'll probably want to link them:

    g++ -o app_name file.o main.o other_file.o
    

    This will create an executable called app_name. But since you are using boost and might have header files located all over the place, you'll probably need additional options. Use -I during the compilation to add a directory to the include path:

    g++ -I/usr/local/more_includes/ -c file.cpp
    

    You'll probably also need to link to some libraries. During linking:

    g++ -L/usr/local/more_libraries/ file.o main.o other_file.o -lsome_library
    

    Now onto makefiles. The basics of a makefile are:

    target: dependencies
        command
    

    For example:

    my_app: file.o
        g++ -o my_app file.o
    
    file.o: file.cpp file.h
        g++ -o file.cpp
    
    clean:
        rm file.o my_app
    

    If you type 'make' it will be default try to create the first target. my_app depends on the target file.o, so it will check to see if file.o has been modified since the last time my_app has been modified. If so, it will relink. In checking file.o, it notices that file.o depends on file.cpp and file.h. If either of those files have been modified since the last time file.o was created, it will recompile that file.

    Targets don't always have to be actual files either. The last one is called clean, it doesn't depend on anything and just deletes file.o and my_app. If you type 'make clean' it will run the command.

    There are of course a ton of other options, but that should get you started.

提交回复
热议问题