Linking Error with UnitTest++ - unit-testing

I am getting an odd Unit Test ++ linking error using Xcode as my ide.
Now I did set the project up with two targets. one target that will do the test the other will do nothing for now.
Note I did link the library and ran the simple working example from the tutorial.
and I compiled it using gcc 4.2.1 from macports.
"std::string::c_str() const", referenced from:
UnitTest::MemoryOutStream::GetText() const in libUnitTest++.a(MemoryOutStream.o)
"std::basic_ostringstream, std::allocator >::str() const", referenced from:
UnitTest::MemoryOutStream::GetText() const in libUnitTest++.a(MemoryOutStream.o)
"std::basic_ostream >::~basic_ostream()", referenced from:
construction vtable for std::ostream-in-UnitTest::MemoryOutStream in libUnitTest++.a(TestRunner.o)
construction vtable for std::ostream-in-UnitTest::MemoryOutStream in libUnitTest++.a(Test.o)
"std::basic_ostream >::~basic_ostream()", referenced from:
construction vtable for std::ostream-in-UnitTest::MemoryOutStream in libUnitTest++.a(TestRunner.o)
construction vtable for std::ostream-in-UnitTest::MemoryOutStream in libUnitTest++.a(Test.o)

Okay so all the problems went away when I switched the std library to GNU standard library rather then the llvm osx standard library. So go to build settings c++ standard library libstdc++ (GNU C++ standard library)

Related

Unable to use Boost in Xcode, keep getting "Undefined symbols for architecture" errors

I'm trying to include Boost in my Xcode project and it seems no matter what I do the project doesn't want to compile.
I get this error every time:
Undefined symbols for architecture x86_64:
"boost::log::v2s_mt_posix::record_view::public_data::destroy(boost::log::v2s_mt_posix::record_view::public_data const*)", referenced from:
boost::log::v2s_mt_posix::record::reset() in Logger.o
"boost::log::v2s_mt_posix::attribute_set::insert(boost::log::v2s_mt_posix::attribute_name, boost::log::v2s_mt_posix::attribute const&)", referenced from:
boost::log::v2s_mt_posix::sources::basic_logger<char, boost::log::v2s_mt_posix::sources::severity_logger<boost::log::v2s_mt_posix::trivial::severity_level>, boost::log::v2s_mt_posix::sources::single_thread_model>::add_attribute_unlocked(boost::log::v2s_mt_posix::attribute_name const&, boost::log::v2s_mt_posix::attribute const&) in Logger.o
(Along with a bunch of others totally 108 errors.)
I've tried a lot to fix this, and read pretty much every question on StackOverflow that is relevant to this.
I am linking it in "Link Binary With Items", I've got libboost_serialization.a and libboost_system.a there (only things in that list).
After installing Boost manually with the ./b2 command, I dragged both of those files into the sidebar. Therefore, the files are in the same directory as my .xcodeproj.
Under Library Search Paths, I have $(PROJECT_DIR), so it should be able to find them.
Am I compiling it with the wrong flags or something?
This solved the problem for me:
Rather than adding libboost_log-mt.* to the ‘Link Binary With Libraries’ section under the ‘Build Phases’ tab, add a path to the archive file (i.e., the .a; dynamic library doesn't work) in ‘Other Linker Flags’ under the ‘Build Settings’ tab:
Other Linker Flags under Build Settings
I have no explanation for why this works; but I've noticed that this technique has worked for me in the past with other libraries ‘not linking’ as well.

How can I resolve single symbol link error when dynamically linking XCode project to lib4cxx library?

I'm writing in C++ under XCode 4.6 on Mountain Lion. I'm trying to add and use the Apache log4cxx library. I installed the library this morning via Brew. I'm linking against liblog4cxx.dylib. I'm getting a link error that just one symbol can't be found:
Undefined symbols for architecture x86_64:
"log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT
const&, std::__1::basic_string,
std::__1::allocator > const&, log4cxx::spi::LocationInfo const&)
const", referenced from:
I know it's finding the library file because if I remove it, I get lots more undefined symbol errors relating to log4cxx.
relevant code is basically:
#include <log4cxx/logger.h>
static LoggerPtr logger(log4cxx::Logger::getLogger("foo.bar.Baz"));
void foo(int p1, int p2)
{
LOG4CXX_WARN(logger, "blah blah blah");
}
Creating the logger object inside the function, either as static or not, doesn't change the behavior. Also, linking with the static library, with or without defining LOG4CXX_STATIC in my project, does not change the behavior.
Looking at the macro I'm calling, I see that this symbol is the actual method that performs the log operation. If take out the logging call but leave in the code that defines the logger object, the code links fine as you might expect.
What do I need to do to have this last symbol resolve?
TIA!
I traced my issue down to compiling the library in a non C++11 compiler, but then my target project was a C++11 compiler.
I was able to compile log4cxx in a C+11 compiler by viewing the changes to log4cxx in the development git repo, which mainly consisted of inserting static_casts, as in this update:
http://apache-logging.6191.n7.nabble.com/C-11-does-not-allow-char-literals-with-highest-bit-set-unless-cast-td34908.html
I suppose the few incompatible routines came up undefined, which is why we were getting confused with only a few seemingly random undefines. (Or I was anyway)

C++ linking problems, seems like I can't link against standard C++ stuff

I am trying to use a library I've compiled myself in an iOS app. The library is the Tesseract OCR lib. It seems like compiling that (and its dependencies) have gone ok.
However, when I try to link against this lib in my app project, the app fails to link. The link errors surprise me; it seems like there are problems with the Tesseract stuff finding pretty standard C++ stuff.
Any suggestions about what I might be doing wrong would be most helpful.
Here is a snippet of the kind of link errors I'm seeing.
Undefined symbols for architecture armv7:
"std::string::find_last_of(char const*, unsigned long) const", referenced from:
tesseract::WordSizeModel::Init(std::string const&, std::string const&) in libtesseract.a(word_size_model.o)
"std::string::find_first_of(std::string const&, unsigned long) const", referenced from:
tesseract::CubeUtils::SplitStringUsing(std::string const&, std::string const&, std::vector<std::string, std::allocator<std::string> >*) in libtesseract.a(cube_utils.o)
"std::string::find_first_not_of(std::string const&, unsigned long) const", referenced from:
tesseract::CubeUtils::SplitStringUsing(std::string const&, std::string const&, std::vector<std::string, std::allocator<std::string> >*) in libtesseract.a(cube_utils.o)
"std::string::data() const", referenced from:
tesseract::CubeUtils::SplitStringUsing(std::string const&, std::string const&, std::vector<std::string, std::allocator<std::string> >*) in libtesseract.a(cube_utils.o)
"std::string::find(char, unsigned long) const", referenced from:
tesseract::TessLangModel::IsLeadingPunc(int) in libtesseract.a(tess_lang_model.o)
Thank you everyone for your answers. I found out what my particular problem was, so will share it here in case anybody else hits it.
My problem was a project build setting. Under "Apple LLVM Compiler 5.0 - Language - C++" there is a setting for "C++ standard library". Its value needed to be changed to "Compiler Default".
Several hours wasted, but problem solved!
I am using a newer version of the iOS SDK and set "Build Settings > Apple LLVM 5.0 - Language - C++ > C++ standard library" to "Compiler Default" but got 46 compile errors.
I got rid of the errors by setting it to "libstdc++ (GNU C++ standard library)".
Hopefully this helps anyone who got stuck when using "Compiler Default".
Not sure if this will help anyone, but I had the same problem when I had a project linked with other projects when one had Base SDK as 5.1 and the other had Base SDK of 7.0.
Maybe you do not have SDK for armv7 architecture, so you can compile your code but standard C++ stuff that installed on your computer is for another architecture (for example x86) and your linker can't find libraries that required for this architecture
The link error suggests that the c++ library cannot be found in the path. Without further information, I would suggest checking the path to the c++ library is correct.

using c++ static library in objective c

I have a static library, whose code was in c++. I want to use it in an objective C app.
(I have been using C libraries preiously, they have been working fine.)
I added the library and tried to build, it couldn't because 'namespace' and vectors were somewhere used in the header files. So, I had to change the type of my objective C file to objective c++. (I didn't change the extension to .mm). Since this file was to be included in another file, that file also had to be changed to c++, similarly for few other files.
Now there is no namespace error. But now when I build, it cribbs that it can't find the referenced symbols. I now changed the extension to .mm, still the same.
I did some searching, I read some things about mangling. I don't understand what that is, but here is something i tried,
Instead of calling the c++ function directly, I created a C function, whose declaration was preceded by 'extern "C"', and the library call was present in this C function. Still the same. I preceded the implementation of the c function by 'extern "C"', still the same.
I also read that if xcode sees .mm extension, only then it uses g++ compiler. and in that case there is no need for extern "C". is it?
Do I need to add some compiler flags in the Other compiler flags target setting?
ld: warning: directory not found for option '-F-F/Users/username/Desktop/projectFolder'
Undefined symbols for architecture armv7:
"IIS::Image::Image::Image(unsigned int, unsigned int, ImageFormat)", referenced from:
-[ImageEditorSupport loadToolKitForImage:width:height:length:] in ImageEditorSupport.o
"IIS::Image::Image::getNumComponents(unsigned int&) const", referenced from:
-[ImageEditorSupport loadToolKitForImage:width:height:length:] in ImageEditorSupport.o
"IIS::Image::ToolKit::adjustSaturation(IIS::Image::Image const&, unsigned int, IIS::Image::Image&)", referenced from:
-[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
"IIS::Image::ToolKit::adjustContrast(IIS::Image::Image const&, unsigned int, IIS::Image::Image&)", referenced from:
-[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
"IIS::Image::Image::Image(unsigned int, unsigned int, ImageFormat, void*, unsigned int)", referenced from:
_create_image_using_buffer in ImageEditorSupport.o
"IIS::Image::ToolKit::adjustColorTemp(IIS::Image::Image const&, int, IIS::Image::Image&)", referenced from:
-[ImageEditorSupport applyToolkitForEditID:intensity:] in ImageEditorSupport.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have also checked several times that i have added the correct library search paths.
I am doubtful about the two '-F-F'. I was expecting only one.
*Edit :*In the warning -F-F/Users/username/Desktop/projectFolder does it mean that it is searching for a path -F/Users/username/Desktop/projectFolder instead of /Users/username/Desktop/projectFolder ? How could that extra -F come?
Seems it hasn't been compiled for armv7 architecture.
You can check with lipo -info myLib.a
The UIViewController in which you're using that c++ library should have the extension .mm instead .m (thats default). Also your main.m should now main.mm
This is because you're using c++ code in Objective-c.

Building static C++ lib for use with Objective-c app in Xcode 4

I found examples and posts to
Create a static C++ lib for use in a C++ application
Create a static Objective C lib for use in an Objective C app.
What I couldn't find and have hassled around with for days now, is the correct way to create a static C++ lib for use in a Objective-C app under XCode 4.
I just want to use this very simple code for testing purposes:
#include <iostream>
#include "myCppLib.h"
using namespace std;
extern "C" void show_the_world() {
cout << "Hello, world!\n";
}
I compile this with armv6/armv7 target, GCC 4.2 compiler, Linking 'C++ Standard Library Type' as 'Static' and have "Symbols Hidden by Default" to YES, as described by the Xcode help for static C++ libs.
My Objective-C app which calls the 'show_the_world' function errors about the std++ lib, which seems not to be included or not correctly referenced:
Undefined symbols for architecture armv6:
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in libmyCppLib_dev.a(myCppLib.o)
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_show_the_world in libmyCppLib_dev.a(myCppLib.o)
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in libmyCppLib_dev.a(myCppLib.o)
"std::cout", referenced from:
_show_the_world in libmyCppLib_dev.a(myCppLib.o)
ld: symbol(s) not found for architecture armv6
collect2: ld returned 1 exit status
I am sure that I compiled the library for the right architecture, so there must be something wrong in another setting or with my code.
Any help is welcome!
Best regards,
jimmy
The correct answer was provided by Jimmy Koerting in the comments: the app needs to be linked against libstdc++.dylib to resolve the standard library symbols.
Thanks to Jimmy Koerting, but one more thing i want to add here is, if you are using xcode latest version with iOS 6.1 please add this libstdc++.6.dylib
Answered in my own comment ;)
the point was to include the std++ lib into the compiling of the resulting objective-c project, too.