Passing C/C++ #defines to makefile

后端 未结 4 588
一向
一向 2020-12-29 20:31

I develop C/C++ using the Eclipse IDE. Eclipse also generates a makefile which I don\'t want to edit as it will simply be overwritten.

I want to use that makefile fo

相关标签:
4条回答
  • 2020-12-29 20:43

    If you are running make from the command line, use

    make CPPFLAGS=-DFOO
    

    which will add -DFOO to all compilations. See also CFLAGS, CXXFLAGS, LDFLAGS in the make manual.

    0 讨论(0)
  • 2020-12-29 20:44

    You could write a small program to include the headers and write a makefile fragment which you include in the main makefile (requires GNU make).

    This is a fairly ugly solution that requires a fair amount of hand hackery. More elegant would be to parse the project file and write the makefile fragment.

    0 讨论(0)
  • 2020-12-29 20:46

    For GCC use -D define.

    OP commented below that he wants to pass the define into make and have it pass it on to GCC.

    Make does not allow this. Typically you just add another make rule to add defines. For instance 'make release' vs 'make debug'. As the makefile creator you make the two rules and have the defines right in the make file. Now if Eclipse is not putting the defines into the makefile for you, I would say Eclipse is broken.

    0 讨论(0)
  • 2020-12-29 20:56

    If you're using autotools another options is to have 2 directories 'bin/debug' and 'bin/release'.

    # Simple bootstrap script.
    
    # Remove previously generated filed and call autoreconf.
    # At the end configure 2 separate builds.
    echo "Setting up Debug configuration: bin/debug"
    ../../configure CXXFLAGS="-g3 -O0 -DDEBUG=1"
    echo "Setting up Release configuration: bin/release"
    cd bin/release/
    ../../configure CXXFLAGS="-O2"
    

    Setup Eclipse. Open the project's properties (Project->Properties->C/C++ Build->Builder Settings) and set the Build Location->Build Directory to

    ${workspace_loc:/helloworld/bin/debug}
    

    Replacing 'helloworld' with your project's directory relative to the workspace (or you can supply an absolute path ${/abs/path/debug}). Do the same thing with the Release config, replacing "/debug" with "release" at the end of the path.

    This method seems like a waste of disk space, but a valid alternative to achieve completely separate builds.

    0 讨论(0)
提交回复
热议问题