How to recompile source file every time while using cmake 2.8.2 in single build for c++11 and c++98 for shared library creation?

前端 未结 2 775
离开以前
离开以前 2021-01-17 03:56

I have a project directory structure of:

Root
  Source
    Common
      MyFolder
      ++ My 3 source files and header 

When I am building

2条回答
  •  孤独总比滥情好
    2021-01-17 04:34

    Most build system assume the compiled objects remain the same within the same pass. To avoid shooting your foot I would suggest telling the build system they were actually different objects, while still compiled from same source files.

    I'm not familiar with cmake but this is how you do with make:

    For example you have a a.cpp which you want to compile 2 times for different compiler options:

    #include     
    int main(int argc, char* argv[]) {
      printf ("Hello %d\n", TOKEN);
      return 0;
    }
    

    And the Makefile would looks like:

    SRC := $(wildcard *.cpp)
    OBJ_1 := $(patsubst %.cpp,%_1.o,$(SRC))
    OBJ_2 := $(patsubst %.cpp,%_2.o,$(SRC))
    
    all: pass1 pass2
    
    pass1: $(OBJ_1)
            gcc -o $@ $(OBJ_1) -lstdc++
    
    pass2: $(OBJ_2)
            gcc -o $@ $(OBJ_2) -lstdc++
    
    %_1.o: %.cpp
            gcc -DTOKEN=1 -c $< -o $@
    %_2.o: %.cpp
            gcc -DTOKEN=2 -c $< -o $@
    
    clean:
            rm -f $(OBJ_1) $(OBJ_2)
    

    What I do here is generate two different list of object from the same source files, which you can even do the same for dependency(-MMD -MP flags).

提交回复
热议问题