Summary terminal value to makefile, how to pass? Simply enter the following command at the terminal, then you can safely use the $ (ABCDE) variable in the makefile file, which has a value of BBB
Makefile
Fun.h
#include <iostream>class Test {public: void static display(const char *str);};
Fun.cpp
#include "fun.h"void Test::display(const char *str) { std::cout <<str;}
Main.cpp
#include "fun.h"int main() {#if defined AAA Test::display("AAA\n");#elif defined BBB Test::display("BBB\n");#else Test::display("CCC\n");#endif return 0;}
Makefile
OBJS = main.o fun.otest: $(OBJS) g++ $(OBJS) -o testmain.o: main.cpp g++ -c main.cpp -D$(abcde) -o main.ofun.o: fun.cpp g++ -c fun.cpp -o fun.oinstall: cp test ~/testclean: rm *.o test
This is an example of a multi-file compile makefile, you can see
fun.h
Never appear in the code, do not doubt,
fun.cpp
The include has come in. First, the terminal value to makefile, how to pass? Simply enter the following command at the terminal, then you can use it in the makefile file with great ease
$(abcde)
This variable, which has a value of
BBB
$make abcde=BBB
Second, makefile file transfer variables to C + + code, in fact, this is a category belonging to g++, and Makefile Independent, simply add parameters on the g++
-D
, as in the previous example
g++ -c main.cpp -D$(abcde) -o main.o
, the terminal passes the command
make abcde=BBB
Pass the variable to makefile,makefile and pass the variable to g++,g++ at compile time define this variable, so main.cpp can use to this variable (should be macro ' #define BBB 1), if just passed a variable name and not assigned value, its value is 1, If you want to assign a value, you should:
abcde=BBB=3
So
g++ -c main.cpp -D$(abcde) -o main.o
It will become:
g++ -c main.cpp -DBBB=3 -o main.o
, in C + + code:
#define BBB 3
Copy the source code from my example, then execute it at the command line: and make abcde=AAA
make abcde=BBB
run the compiled program to see how it differs.
Makefile Third: terminal transfer value to makefile, makefile to C + + code