Makefile Third: terminal transfer value to makefile, makefile to C + + code

Source: Internet
Author: User

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.hNever appear in the code, do not doubt, fun.cppThe 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=BBBPass 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.oIt 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.