Making a cross platform library with CMake?

2019-06-25 01:00发布

问题:

Is CMake difficult to use? I've been developing a library using Windows and MSVC++ . I tested my code in Linux and OSX by tediously making objects from each file and making a library out of this.

I have several Directories ex:

Agui
-> Widgets
      ->Button
      ->ListBox
 -> Backends
        -> Allegro

And what I'd like is, for example, if I'm on Windows, it auto generates an msvc project with all these directories and files included and ready to be compiled.

On Linux and OSX I'd like to be able to do something like

cmake

make

is this possible with CMake, and easy to do given my situation. Thanks

回答1:

Yes, that is exactly the reason CMake is made for. Huge projects like KDE use CMake. And its easy to setup.



回答2:

This is exactly what CMake was developed to do. CMake generates native build files and workspaces that can be used on your platform of choice. So on Unix this is normally Unix Makefiles, on Windows Visual Studio you can select your Visual Studio version to generate a solution that can be opened in the IDE, or use NMake Makefiles.

If you want to add a simple library, you just use the add_library function, giving it a library name and a list of source files. I would recommend taking a look at the CMake tutorial for a brief overview. There is also a book, Mastering CMake, along with many tutorial pages. You can ask the CMake command line for help too, 'cmake --help-command add_library' would show you the add_library documentation.



回答3:

CMake will generate project files for a number of IDEs, but I find that automake+libtool tend to be easier to use. Here is an excellent tutorial: http://www.lrde.epita.fr/~adl/autotools.html

Don't be put off by the page count: most of it is step-by-step revealing of diagrams.