OS X (Command Line)

From Crypto++ Wiki
Jump to navigation Jump to search

This page will provide instructions for compiling Crypto++ on the command line for OS X. OS X is Apple's desktop operating system. Building the library for OS X is like building for Linux. There is nothing special, and it should work out of the box.

The one caveat to building from the command line is, the build lacks -stdlib=libc++. LLVM's libc++ should be added to CXXFLAGS if needed. Xcode uses libc++ by default, so you should add it if you are linking to the library from an XCode project.

The library is regularly tested on OS X 10.5 on PowerPC, and OS X 10.7 though 10.10 on X86_64 machines. OS X 10.13 is also tested daily using Travis CI.

Related pages are iOS (Command Line) and iOS (Xcode).

Makefile

The library is built for OS X using the GNUmakefile. Apple's Command Line Tools happily consumes the makefile. Running make out of the box builds libcrypto.a and cryptest.exe.

$ cd cryptopp 
$ make -j 4
c++ -DNDEBUG -g2 -O3 -DCRYPTOPP_DISABLE_MIXED_ASM -fPIC -pthread -pipe -c cryptlib.cpp
c++ -DNDEBUG -g2 -O3 -DCRYPTOPP_DISABLE_MIXED_ASM -fPIC -pthread -pipe -c cpu.cpp
c++ -DNDEBUG -g2 -O3 -DCRYPTOPP_DISABLE_MIXED_ASM -fPIC -pthread -pipe -c integer.cpp
...
c++ -o cryptest.exe -DNDEBUG -g2 -O3 -DCRYPTOPP_DISABLE_MIXED_ASM -fPIC -pthread
 -pipe adhoc.o test.o bench1.o bench2.o bench3.o datatest.o dlltest.o fipsalgt.o
 validat0.o validat1.o validat2.o validat3.o validat4.o validat5.o validat6.o va
lidat7.o validat8.o validat9.o validat10.o regtest1.o regtest2.o regtest3.o regt
est4.o ./libcryptopp.a

You can build the shared object using the dynamic target:

$ make dynamic
...
c++ -dynamiclib -o libcryptopp.dylib -DNDEBUG -g2 -O3 -DCRYPTOPP_DISABLE_MIXED_A
SM -fPIC -pthread -pipe -install_name "libcryptopp.dylib" -current_version "8.3.
0" -compatibility_version "8.3" -headerpad_max_install_names cryptlib.o cpu.o
integer.o 3way.o adler32.o algebra.o algparam.o allocate.o arc4.o aria.o ...

CXXFLAGS

The default build uses -DNDEBUG -g2 -O3 -fPIC. You can add other options in two ways. First you can open the makefile and add the option to CXXFLAGS. Second, you can supply CXXFLAGS on the command line. The examples below show you how to change CXXFLAGS on the command line.

libc++

If you are working with XCode then you should add -stdlib=libc++ to CXXFLAGS in the makefile. You can also enable -stdlib=libc++ from the command line:

$ CXXFLAGS="-DNDEBUG -g2 -O3 -stdlib=libc++ -fPIC" make -j 4
c++ -DNDEBUG -g2 -O3 -stdlib=libc++ -fPIC -DCRYPTOPP_DISABLE_MIXED_ASM -pthread
-pipe -c cryptlib.cpp
c++ -DNDEBUG -g2 -O3 -stdlib=libc++ -fPIC -DCRYPTOPP_DISABLE_MIXED_ASM -pthread
-pipe -c cpu.cpp
c++ -DNDEBUG -g2 -O3 -stdlib=libc++ -fPIC -DCRYPTOPP_DISABLE_MIXED_ASM -pthread
-pipe -c integer.cpp
...

Debug

You can build a Debug version of the library using -DDEBUG -g3 -O0.

$ CXXFLAGS="-DNDEBUG -g3 -O0 -fPIC" make -j 4
c++ -DNDEBUG -g3 -O0 -fPIC -DCRYPTOPP_DISABLE_MIXED_ASM -pthread -pipe -c cryptl
ib.cpp
c++ -DNDEBUG -g3 -O0 -fPIC -DCRYPTOPP_DISABLE_MIXED_ASM -pthread -pipe -c cpu.cp
p
c++ -DNDEBUG -g3 -O0 -fPIC -DCRYPTOPP_DISABLE_MIXED_ASM -pthread -pipe -c intege
r.cpp
...

CRYPTOPP_DISABLE_MIXED_ASM

CRYPTOPP_DISABLE_MIXED_ASM is needed because Clang cannot handle mixed ASM, meaning the use of both AT&T style and Intel style assembly language. Also see LLVM Issue 39895, Error: unknown token in expression using inline asm.

The makefile adds CRYPTOPP_DISABLE_MIXED_ASM automatically for Clang compilers. If you are working with XCode then you should add CRYPTOPP_DISABLE_MIXED_ASM to config.h or XCode's preprocessor defines.

Multi-arch

You can build a fat version of the library by adding -arch i386 -arch x86_64 to CXXFLAGS.

$ CXX=clang++ CXXFLAGS="-DNDEBUG -g2 -O3 -arch i386 -arch x86_64" make -j 4
clang++ -DNDEBUG -g2 -O3 -arch i386 -arch x86_64 -DCRYPTOPP_DISABLE_MIXED_ASM -f
PIC -pthread -pipe -c cryptlib.cpp
clang++ -DNDEBUG -g2 -O3 -arch i386 -arch x86_64 -DCRYPTOPP_DISABLE_MIXED_ASM -f
PIC -pthread -pipe -c cpu.cpp
clang++ -DNDEBUG -g2 -O3 -arch i386 -arch x86_64 -DCRYPTOPP_DISABLE_MIXED_ASM -f
PIC -pthread -pipe -c integer.cpp
...

After the build you can check architectures.

$ lipo -info libcryptopp.a
Architectures in the fat file: libcryptopp.a are: i386 x86_64

$ lipo -info cryptest.exe
Architectures in the fat file: cryptest.exe are: i386 x86_64

And finally test the build using arch program. Notice sizeof(word) changes depending on i386 and x86_64.

$ arch -x86_64 ./cryptest.exe v
Using seed: 1571297305

Testing Settings...

passed:  Your machine is little endian.
passed:  Aligned data access.
passed:  sizeof(byte) == 1
passed:  sizeof(word16) == 2
passed:  sizeof(word32) == 4
passed:  sizeof(word64) == 8
passed:  sizeof(word128) == 16
passed:  sizeof(hword) == 4, sizeof(word) == 8, sizeof(dword) == 16
passed:  cacheLineSize == 64
...

And:

$ arch -i386 ./cryptest.exe v
Using seed: 1571297320

Testing Settings...

passed:  Your machine is little endian.
passed:  Aligned data access.
passed:  sizeof(byte) == 1
passed:  sizeof(word16) == 2
passed:  sizeof(word32) == 4
passed:  sizeof(word64) == 8
passed:  sizeof(hword) == 2, sizeof(word) == 4, sizeof(dword) == 8
passed:  cacheLineSize == 64
...

Xcode

The wiki does not have a "OS X (XCode)" article at the moment because we don't have access to Xcode. Xcode users should keep the following in mind if you are creating a XCode project for Crypto++.

First, Xcode is a build system that displaces the Makefile. The Makefile compiles the library for you, and XCode needs to duplicate the functionality. Functionality to duplicate includes preprocessor defines, compiler options for certain source files, and producing static and dynamic libraries.

If you have an open question on a preprocessor define or how to build a particular source file, then run the makefile from the command line.

Second, you can get a list of source files to include in your XCode project by running make sources in a terminal.

$ make sources
***** Library sources *****
cryptlib.cpp cpu.cpp integer.cpp 3way.cpp adler32.cpp ...

***** Library headers *****
3way.h adler32.h adv_simd.h aes.h ...

***** Test sources *****
adhoc.cpp test.cpp bench1.cpp bench2.cpp bench3.cpp ...

***** Test headers *****
bench.h factory.h validate.h ...

Third, your XCode project only needs "Library sources" and "Library headers". You don't need the test sources per se. You should build the test project and run the self tests, but you won't need the test program when building your project.

The Visual Studio solution separates them, too. In Visual Studio the "Library" project is called "cryptlib", and the "Test" project is called "cryptest".

Fourth, Clang cannot handle mixed ASM, so the preprocessor defines should include CRYPTOPP_DISABLE_MIXED_ASM.

Fifth, *_simd.cpp source files require additional options. For example, rijndael_simd.cpp requires -msse4.1 -maes for i386 and x86_64 builds; and -march=armv7-a -mfpu=neon for ARMv7 builds. The [mostly complete] list of source files with options can be found at BASE+SIMD.

If you don't want to manage separate compiler flags for *_simd.cpp source files, then add CRYPTOPP_DISABLE_ASM to the preprocessor defines.

Sixth, Simulator builds should include CRYPTOPP_DISABLE_ASM.

Finally, Xcode uses LLVM's C++ runtime library by default, a.k.a -stdlib=libc++ (as opposed to GNU's runtime library, a.k.a -stdlib=libstdc++). You don't need to do anything special for it.