12

I'm trying to create an Open Source C++ project. I want it to be as easy to build as possible, but at the same time cross platform. I don't need gui or heavy libraries like boost or Qt, so I've settled on using GitHub, CMake, and LibSourcey. My problem is, I can't find a way to make my project easy to build in windows, which is my development environment. How can I "make install" a library in Windows for use in my project? Do I even have to install it in windows? Is it possible to download, build, and link it automatically?

On windows, besides an installer, I also want to make a portable version, so don't want any hard coded library paths.

I assume, on some platforms, like Linux, libraries are built separably and packaged up by maintainers. So I shouldn't just bundle up my own copies.

So, my question is: How can I set up a project that is cross platform and easy to build, and what are the best practices?

denisps
  • 131
  • 1
  • 8
  • I am not sure if you are talking about finding a solution for your development process or for making releases or both? [msys2](https://sourceforge.net/projects/msys2/) can be used under windows to manage your required packages. – x squared May 11 '16 at 19:39
  • I hit the same problem lately, msys2 look great, but I prefer using native toolchain for windows developement. – Guillaume Racicot May 11 '16 at 19:57
  • I looked into msys, but I want it to be native. With msys it's even harder to compile for x64 and with proper unicode support. – denisps May 11 '16 at 20:01
  • MSYS2 provides native and POSIX toolchains. – Ray Donnelly May 12 '16 at 08:45
  • @GuillaumeRacicot The question is too broad, more details should be given. – Antonio May 20 '16 at 12:56
  • Should I open another question for my similar problem then? – Guillaume Racicot May 20 '16 at 14:03
  • 4
    This question is indeed rather broad, in that it asks both about the dev side as well as the deployment side. @GuillaumeRacicot - given your bounty description, I'd say another question with your details (export, etc.) would really make more sense. – Martin Ba May 22 '16 at 19:58
  • A make install on Windows is not intrinsically different from a make install on Linux. It creates a "deployment" version of a library, i.e. copying over the binaries, perhaps headers, etc. but leaves behind all the "build" cruft not necessary after deployment. If you generate a Visual Studio project it will also generate an INSTALL project which you can use as a target for "deployment". The equivalent of 'make install' is then to build the INSTALL target (or call 'msbuild INSTALL.vcxproj') – André May 23 '16 at 12:14

4 Answers4

2

You can create git submodule in your git repo with path for example contrib/LibSourcery and url to github repo of LibSourcery, because of LibSourcery uses cmake you just need add such line into your CMakeLists.txt: add_subdirectory(contrib/LibSourcery)

So person who want to use your code, just do git clone --recursive url to get all of your code and dependencies, after that it run cmake -G, to create project for IDE of his choice (including MSVC++ of course), open project in IDE and press build button, that's all.

fghj
  • 8,898
  • 4
  • 28
  • 56
  • It doesn't seem to work. I don't know if I'm doing something wrong or LibSourcery doesn't support this type of inclusion. I did include it in contrib folder as submodule, but I'm still not sure if that's the right way to include a library on Unix platforms. Would it link to the installed library if it's available? How do it add includes? – denisps May 12 '16 at 19:22
  • @denisps cmake also support external projects in such way https://cmake.org/cmake/help/v3.0/module/ExternalProject.html – fghj May 12 '16 at 19:24
  • This answer seems to be the closest. Unfortunately, LibSourcery seems to be broken and potentially abandoned. I would appreciate if you elaborate on how to take into account the case when I'm linking to the library that is already installed on, for instance, Linux. – denisps May 24 '16 at 19:42
  • 1
    @denisps In case of already installed library there is no universal answer. In the most simple case, when you work on `Linux`, headers of library in `/usr/include` and compiled code in `/usr/lib` you need just add `-llibname` into `target_link_libraries`. If something more complex, for example you want not only `Linux` support, or library install files in sub-directories of `/usr/lib` `/usr/include`, you need to use `find_package`, `find_library` and other stuff, and how you should handle this situtation depend on library, and fact that some one from cmake community work with it or not. – fghj May 24 '16 at 20:15
1

Use babun. It's a wrapper for cygwin and it works perfectly for everything I need, even compiling with cmake.

Alternatively, you could use premake, which uses lua as a config system and works fine on windows.

ar1a
  • 374
  • 3
  • 10
0

There is no elegant cross-platform way, since the idea of "make install" doesn't exist on Windows, therefore the use of cmake install is undefined there. For something which is supposed to help cross-platform, I feel this is a deficiency w cmake.

My solution is to write a custom _INSTALL which takes the same args as cmake install and then on Linux it just calls install, and on Windows it does an add_command which does a post-build copy to the install paths, which accomplishes the same thing. Basically, _INSTALL behaves the way you expect a cross-platform install command to behave. Can share my _INSTALL func if there is interest.

_INSTALL is placed nto a Helper.cmake, and included in all my CMakeList.txt for my projects, so all I need to do is call it and the generated lib/inc files magically appear for both win and linux.

ramakarl
  • 1
  • 1
0

You can use vcpkg, an open source package manager for c and c++. It allows to easily download and compile libraries and then use find_package from within CMake like you would on linux. It's very easy to use. It even provides hints as to how to alter your cmake file to use the libraries.

I started by installing packages with the command line, and then wondered why they wouldn't show up in visual studio. But I realized that it would download 32 bit libraries by default. Use .\vcpkg install <libname>:x64-windows if you need the 64 bit libraries.

After running the integrate command, you will need to delete any cmake caches to have MSVS use the new toolchain.

FalcoGer
  • 2,278
  • 1
  • 12
  • 34