I’ve been toying with using the Microsoft CPPRestSDK library to practice with REST services and see how easily they’re done in C++. Given that I’d like the project to run on more than one OS, I thought it’d be a good idea to use CMake to build it in several places. This, of course, has required me to refresh my knowledge of CMake. And, of course, I’ve run into a problem with CMake: Getting it to generate a Makefile or XCode project on MacOS that builds and links against C++11.
I am writing a cross platform application using CMake that builds on Mac OS X. I just upgraded to CMake 2.8-1. When I configure and build my project on my Power Mac G5 system running Mac OS X 10.5.8, the project builds just fine. When I try to configure the project on my MacBook Pro running Mac OS X 10.6.3, I get link errors because the project has been configured to build 64-bit applications. Cmake does not find openssl on Mac OS X El Capitan #27. Closed ralight opened this issue Mar 15, 2016 0 comments Closed cmake does not find openssl on Mac OS X El Capitan #27. Ralight opened this issue Mar 15, 2016 0 comments Labels. Copy link Quote reply. Use CMake 3.12.2 - it is known to be working and is supported (if you want to use CMake already installed on your system, 2.8.12.2 is known to work on Mac OS.
One big note that I’ll make here is that this post is a description of a problem that I’m having. I will post links to this post in various places where I think I might find answers other than Google, StackOverflow, and the CMake docs where I’ve already looked. If I get a good answer from somebody I will update this post to reflect the changes made.
Problem Solved!
It turns out that I had a misunderstanding of how CMake expected targets to be declared. It expects all properties related to a target before you declare that target with
add_executable
or add_library
. I have updated the CMakeLists.txt to have the properties set before the add_executable
call and, lo and behold, it works appropriately. I would like to thank the folks in the CPPLang Slack community for helping me identify that this was the problem. Please see the conclusion for more thoughts on this. What does the CMakeLists.txt look like?
This, of course, is likely the first question that comes to mind for anybody who knows CMake well. You can find the project here and the CMakeLists.txt here. Given that my project is relatively simple (so far) I’m attracted to the idea that Craig Scott posted. That is to use the following settings:
As you can see in my CMakeLists.txt I’ve got these set. You’ll also notice that they’re only set for MSVC. Everybody else gets the following to set
-std=c++11
directly:Why don’t I use CMAKE_CXX_STANDARD for MacOS?
This is setup this way because, at present, using the CMake variables did not set the command line options correctly for clang on MacOS. If I use CMAKE_CXX_STANDARD to generate Makefiles on MacOS here are the beginning of the compile time errors. These come from the CPPRestSDK headers and are only errors if you’re not compiling with C++11 or above:
You can see the same error displayed in Xcode if I generate an Xcode project and build with it:
Why doesn’t CMAKE_CXX_STANDARD work for me on MacOS?
That is a very good question! I’ve done some googling for how to set the C++ standard to C++11. Most of the pages I’ve found have recommended using the CMake variables to set the standard and, if that doesn’t work, to just set the appropriate flags for the command line. You can see in the CMakeLists.txt that this is what I’ve ended up doing.
Mac Operating System Download
These flags are, of course, not portable. This defeats some of the purpose of using CMake. I know there will always have to be some settings that are OS specific but if there’s a way to use CMake to set something for all platforms in a generic manner I’d prefer to do that.
Conclusion
I have re-written this section now that I’ve solved this problem. The Cmake documentation surrounding variables describes that they are dynamically scoped. If add_executable reads the variables at the time it is executed and it is what generates all of the command line flags for the compiler then it makes sense that the variables must be set beforehand.
So, in short, my misunderstanding is where in the CMake process the compile time properties are set. The way it is implemented means that these properties only have to be generated once. This is more efficient than having to regenerate them whenever a variable is changed. I did not find anywhere in the CMake documentation that explicitly warns users to have variables set before creating the target. But, to be fair, I haven’t read ALL the documentation. I hope this helps you in your endeavors with CMake.
Specify the minimum version of the target platform (e.g. macOS or iOS)on which the target binaries are to be deployed. CMake uses thisvariable value for the
-mmacosx-version-min
flag or their respectivetarget platform equivalents. For older Xcode versions that shippedmultiple macOS SDKs this variable also helps to choose the SDK in caseCMAKE_OSX_SYSROOT
is unset.If not set explicitly the value is initialized by the
MACOSX_DEPLOYMENT_TARGET
environment variable, if set,and otherwise computed based on the host platform.The value of this variable should be set prior to the first
project()
or enable_language()
command invocationbecause it may influence configuration of the toolchain and flags.It is intended to be set locally by the user creating a build tree.This variable should be set as a CACHE
entry (or else CMake mayremove it while initializing a cache entry of the same name).Despite the
OSX
part in the variable name(s) they apply also toother SDKs than macOS like iOS, tvOS, or watchOS.Cmake For Mac Os 10.10
This variable is ignored on platforms other than Apple.