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?

混江龙づ霸主 提交于 2019-12-01 12:28:16

问题


I have a project directory structure of:

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

When I am building my project it generates 3 to 4 shared libraries. Lib1 compiled using c++98 and others using c++11. Flags are added in CmakeList.txt which is at root. I need my 3 source files to be compiled for Lib1 and for other Libs as as well. but here what happens is compiler is first compiling my source file for lib using c++11 and then it is trying to use same .o file for Lib1 as well. So for .o file which is generated using c++11 is throwing exception when same is used for c++98 compiled library.

So how do write this in CmakeList.txt such that compiler rather than trying to use same .o file will compile source file again for Lib1(c++98 compiled library)

Is there any flag I can specify so that it won't take precompiled .o file and will compile it again ?

Here flags are not being overridden for different shared libraries but actually same object file by make file is being used for different flags


回答1:


This is sort of counter to how makefiles and cmake usually work.

Most users consider it really important that make performs an incremental build.

The usual way with makefiles is to do make clean which is supposed to remove any binaries and object files that were created.

However, sometimes I write cmake scripts that use globbing over the source directory to assemble the project. (That means, it says "just grab all *.cpp files in the /src folder and make an executable from them".) A makefile cannot check what files in a directory, so the make build will be broken after I add a new file, and make clean won't fix it -- the whole makefile will need to be regenerated by cmake.

Usually what I do is, I write a simple bash script, named rebuild.sh or something,

#!/bin/bash
rm -rf build
mkdir build
cd build
cmake ..
make -j3
./tests

And I put that in the root of my repository, and add /build to my .gitignore. I call that when I want to do a full rebuild -- it nukes the build directory, so its foolproof. When I want an incremental rebuild, I just type make again in the /build directory.

The rebuild.sh script can also serve a double purpose if you use travis-ci for continuous integration.




回答2:


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 <stdio.h>    
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).



来源:https://stackoverflow.com/questions/38897195/how-to-recompile-source-file-every-time-while-using-cmake-2-8-2-in-single-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!