类 Maven 的 C + + 依赖管理?

假设我有一个分为几个子项目的 C + + 项目。子项目都生成一个 DLL,不同的开发团队负责每个子项目。现在,如果我想构建主项目,有没有一种方法可以避免必须自己构建所有子项目?

简而言之,我正在寻找一种类似于 Maven 对 Java 所做的依赖管理(即对二进制文件和头文件)的方法。

事实上,我尝试使用 Maven 来完成这个任务,但是这个任务相当麻烦,因为我必须手动创建包,而且非常频繁,所以 Maven 没有选择最近的更改。另外,运行编译有点麻烦,因为我必须在 Maven 内部调用 NAnt (我使用 NAnt 的特性直接构建 Visual Studio 解决方案)。

对于如何做到这一点,有什么提示和想法吗?

70185 次浏览

I recommend to use the mother of all build dependency systems: make.

If you only want dependency management, try Ivy, it integrates nicely with Ant (and I assume NAnt can do the same based on this blog, which is linked from the Ivy site).

There is also Byldan, a .Net version of Maven. Don't know how well that will work for you though.

Make and GCC are a great combo for really good dependency checking.

GCC can generate 'make' dependency files automatically (-MD commandline switch), so as to be able to rebuild all sourcefiles that depend upon a given header, for example.

I have some simple rules that I cut-n-paste into my makefiles:

# compile c files
%.o:    %.c
${CC} ${CFLAGS} -c $< -MD -MF $(<:%.c=%.dep) -o $@


# compile c++ files
%.opp:  %.cpp
${CPP} ${CPPFLAGS} -c $< -MD -MF $(<:%.cpp=%.dep) -o $@

Now if your object files are declared in say an OBJ_C and an OBJ_CPP list:

.PHONY: cleandep
cleandep:
rm -f $(OBJ_C:%.o=%.dep) $(OBJ_CPP:%.opp=%.dep)


-include $(OBJ_C:%.o=%.dep) $(OBJ_CPP:%.opp=%.dep)

Make can of course track dependencies with other projects and such, e.g. rebuilding a shared libary as necessary, too.

For example, if your other teams always put their latest DLLs on some shared folder:

myapp: ${SRC_CPP} ${LIB_DIR}other_team.lib
...


${LIB_DIR}other_team.lib: /shared_folder/latest/other_team.lib
cp /shared_folder/latest/other_team.lib ${LIB_DIR}other_team.lib

Initial Answer: I would suggest using CMake. It is a multi-platform make file generator (generates Visual Studio or Eclipse CDT projects as well).

http://www.cmake.org/

I did really good experience with it. The best thing I like about it was the ability to produce generic project structure. So you can generically include sub-projects look-up for unit tests etc. without changing the script every time.

They have also lots of modules on how to find pre-installed build libraries, required for the project (like Boost, QT etc.)


Update: In the mean time there was some effort to introduce package management for C++. Some projects worth looking at:

  • conan.io integrates with major build tools:
    • CMake
    • Visual Studio
    • Makefile
    • XCode
    • ...
  • cpm based on CMake (Note CPM is not being actively maintained.)
  • Buckaroo

Note as pointed out by @RAM in the comments cpm is no longer actively maintained.

Try scons, you will be hooked. Make is outdated, difficult and expensive to maintain.

You can create NuGet package for used libraries and use NuGet for dependency management.

See also, NuGet for C++

For the dependency management, it exists a new project (it is a startup company) which is implementing this type of tool: https://github.com/biicode (a C++ dependency manager). You could add your dependencies and it should work.

Currently, the project's name is conan.io, they were acquired by JFrog.

UPDATE: The project is dead... Unfortunately, it seems the startup couldn't get enough premium paying customers, but the server seems is working fine...

UPDATE2: It seems there is a substitute project: conan.io (thanks @mucaho)

Try SCons

SCons is an Open Source software construction tool—that is, a next-generation build tool. Think of SCons as an improved, cross-platform substitute for the classic Make utility with integrated functionality similar to autoconf/automake and compiler caches such as ccache. In short, SCons is an easier, more reliable and faster way to build software.

I recommend the following high-level build systems:

Edit:

Biicode is deprecated

Alternative: Conan.io

There is a number of tools sitting on top of SCons, providing higher-level functionality similar to that of Autotools which are trying to make the developers life easier (e.g. WAF, SNOCS). Unfortunately, SCons itself has the major drawback - longer compilation time for the large projects.

I can recommend to try out SNOCS (which is a SCons reversed) for those of you looking for an easy dependency management and choosing compilation options in the single command (compiler, x86/x64, Debug/Release, static/shared libraries, test/install targets, etc.).

SNOCS also tries to tackle the long compilation time problem by storing the projects configuration output in the separate files, which allows the consequent builds to skip configuration phase altogether and go straight to the building phase (last feature is under construction now)

CMake's configuration becomes tedious in a larger solutions, so the build system maintenance takes a large fraction of the developer time. Luckily as Martijn already mentioned there is biicode which "uses CMake to generate your project with its dependencies".

I recommend conan, which I was using these days. It's very powerful to maintain all the dependent libraries and binaries in your project.