简体   繁体   中英

How can I statically link a C++ library to a C application in OSX?

I've looked through the other similar questions about this topic, and I think there's something OSX-specific I'm missing.

I'm trying to statically link Basho's fork of LevelDB to my C application (since packaged versions of LevelDB will use Google's mainline). Even using the c++ linker and adding -lc++ to the command line, I still get flooded with errors about missing c++ STL symbols such as:

"std::string::_Rep::_M_destroy(std::allocator const&)", referenced from: _leveldb_open in libleveldb.a(co)

The strange thing is, I know I have the C++ bits enabled right because I'm also linking to libsnappy, and before I added the link directive there was unresolved libsnappy symbols (obviously) that are now cleared up.

As far as I know, I have the latest version of xcode & the rest of the compiler chain. libleveldb is also compiled using the same toolchain as my regular application. Here's the command line I'm trying:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -g -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/get_rid_status.dir/get_rid_status.co -o get_rid_status /usr/local/lib/libdb.a /usr/lib/libssl.dylib /usr/lib/libcrypto.dylib /usr/lib/libaprutil-1.dylib /usr/lib/libapr-1.dylib -lpthread -ldl -lm -lz leveldb/libleveldb.a /usr/local/lib/libsnappy.dylib -lc++

I'm linking with the c++ linker and adding the c++ standard library with -lc++ to no avail.

UPDATE Found the correct answer, which can be found below. Kudos to the users who told me the right etiquette when I originally answered my own question here.

Turns out, leveldb adds some extra linker flags I didn't notice at first. The culprit is -mmacosx-version-min=10.8 . Adding that flag to the link step of my C application got everything to line up.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM