Makefile Conditional compilation for debug and release

Source: Internet
Author: User

Generally, the debug version is used in the development and testing phase, and the release version is used for release.

You can use makefile to customize and compile different versions to avoid modifying programs and makefile files.

I read some documents and found a solution. The makefile predefine macro and condition judgment combined with the make predefine variable for Conditional compilation.

 

For example, there is a test. cpp that contains this code

#ifdef debug//your code#endif

You want to execute it in the debug version, but not in the release version.

We can write such a makefile:

 1 ver = debug 2  3 ifeq ($(ver), debug) 4 ALL: test_d 5 CXXFLAGS = -c -g -Ddebug 6 else 7 ALL: test_r 8 CXXFLAGS = -c -O3  9 endif10 11 test_d: test.do12     g++ -o $@ $^13 14 test_r: test.ro15     g++ -o $@ $^16 17 %.do: %.cpp18     g++ $(CXXFLAGS) $< -o $@19 20 %.ro: %.cpp21     g++ $(CXXFLAGS) $< -o $@

To put it simply, makefile defines different compilation options for cxxflags and the output program all according to Ver,

The output program of the debug version is test_d, and the output program of the release version is test_r.

The debug version compilation option is-C-g-ddebug, And the release version compilation option is-C-O3"

In debug, the suffix of the object file is ". Do", and in release, the suffix of the object file is ". ro"

The debug version compilation option uses "-d" to define macro debug so that the your code can be executed.

Different Versions of compilation options, object files, and output programs are different. Therefore, you can compile two versions of programs at the same time without affecting each other.

 

When makefile is executed, first determine the ver variable. If the ver value is debug, compile the debug version; otherwise, compile the release version. Of course, the debug version is compiled by default.

What should I do if I want to compile the release version?

When you execute make, assign a value to the ver variable so that the ver value is not debug, for example

# make ver=release

 

 

If you have any omissions, please correct them!

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.