Some problems when using Boost Library on Xcode - xcode

I use Xcode 4.5.2 and I wonna use Boost, but I got some problems.
In Build Setting, if I choose libc++ (LLVM C++ standard library with C++11 support), I will get the error messgae "Apple Mach-O Linker (ld) Error".
Just like this:
Undefined symbols for architecture x86_64:
"boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::codecvt<wchar_t, char, __mbstate_t> const&)"
referenced from:
boost::filesystem::path::path<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, void>::type*) in test1 - inverted index.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I include these two headers:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
I also add these in Build Phases:
libboost_filesystem-mt.dylib
libboost_filesystem-mt.a
libboost_system-mt.dylib
libboost_system-mt.a
Code is here:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace boost::filesystem;
std::map<std::string, std::set<int>> invertedIndex;
std::map<std::string, int> number;
bool check_char(const char in)
{
if((in>='A' && in<='Z') || (in>='a' && in<='z'))
return true;
else
return false;
}
int main() {
int con = 0;
std::string word;
path root("/Users/tomhu/Desktop/pro/data/");
std::string rootDirectory = root.native();
recursive_directory_iterator iter(root);
recursive_directory_iterator end;
for (; iter != end; ++iter)
{
if(is_regular_file(*iter))
{
std::string filename;
std::string directory(rootDirectory);
filename = iter->path().filename().native();
directory.append(filename);
std::ifstream fileIn(directory.c_str());
number[filename] = con;
if(!fileIn)
{
std::cerr << "File doesn't exist!" << std::endl;
exit(1);
}
while(fileIn>>word)
{
long po = 0;
if(!check_char(*(word.end()-1)))
word.pop_back();
transform(word.begin(),word.end(),word.begin(),::tolower);
if(word=="i")
word="I";
invertedIndex[word].insert(con);
}
}
}
std::cout << con << std::endl;
return 0;
}
!!!!!!!!
If I choose libstdc++ (GUN C++ standard library) in "Build Setting", I won't get any error message about Boost, but I can't use anything in C++11 Standard.
How to solve these problems?

See Why can't clang with libc++ in c++0x mode link this boost::program_options example? - Howard gave an excellent answer what's going wrong there.
And here is how you can recompile Boost for clang+libc++: How to compile/link Boost with clang++/libc++?

Related

Linker Error: Undefined Reference to Function

I'm running into a linker error when I try to compile my code.
When I compile using g++ *.cpp -std=c++11 -o run I get the following error:
main.cpp:(.text+0x355): undefined reference to `Actions(mBoard&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)'
collect2: error: ld returned 1 exit status
I've tried compiling and linking all the files separately to no avail. Its not a member function so it's not a label issue. I've also tried to use g++ -std=c++11 main.cpp mAI.cpp -o run to make sure it is in fact compiling and linking both files but no luck.
What's really making me crazy is that it's not complaining about the Translate function that's declared and defined in the same way as the Actions function.
Any help is greatly appreciated.
mAI.hpp
#ifndef MAI_HPP
#define MAI_HPP
#include <iostream>
#include <set>
#include <tuple>
#include "mBoard.hpp"
using namespace std;
void Actions(mBoard const &state, string const playerColor, set<string> moveList);
tuple<int, int> Translate(string index);
#endif
mAI.cpp
#include "mAI.hpp"
std::set<char> blackPieces = {'r', 'n', 'b', 'q', 'k', 'p'};
std::set<char> whitePieces = {'R', 'N', 'B', 'Q', 'K', 'P'};
void Actions(mBoard const &state, string const playerColor, set<string> moveList)
{
//Do some stuff
}
tuple<int, int> Translate(string index)
{
//Do different stuff
}
main
#include "mAI.hpp"
#include "mBoard.hpp"
#include <set>
#include <tuple>
#include <string>
#include <iostream>
int main()
{
mBoard dasBoard;
set<string> moveList;
string color = "White";
cout<<"TEST - Translate: f6 to file, rank: ";
tuple<int, int> test;
test = Translate("f6");
cout << get<0>(test) << ", " << get<1>(test) << endl;
Actions(dasBoard, color, moveList);
return 0;
}
That sounds like a very old gcc compiler. That was a problem during abi change in earlier versions of gcc and installed libraries on the system.
using -D_GLIBCXX_USE_CXX11_ABI=0 and recompile all maybe help.
See also this documentation:
https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.html
BTW:
I can compile and link your code with gcc 7.3.1 ( fedora ) without any error. Some warnings of unused vars after cut and paste your code and only add an empty aBoard class. So I believe there is nothing wrong with your code.
From the documentation:
If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.

C++ linker error after overloading an operator

I keep getting the following error when I try to compile an example program from C++ how to program, Deitel and Deitel. I used g++ Fig11_05.cpp -o Fig11_05
I spent hours trying to solve this problem by looking up the Internet and particularly stackoverflow, but with no avail!
I tried using different command line arguments such as -libstd=libc++, -std=c++11, -std=c++14
the error I keep getting is as this:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from:
_main in Fig11_05-1f04bd.o
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from:
_main in Fig11_05-1f04bd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The result of g++ -v:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The codes:
// Fig. 11.3: PhoneNumber.h
// PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
#include <string>
class PhoneNumber
{
friend std::ostream& operator<<(std::ostream&, const PhoneNumber&);
friend std::istream &operator>>(std::istream&, PhoneNumber&);
private:
std::string areaCode; // 3-digit area code
std::string exchange; // 3-digit exchange
std::string line; // 4-digit line
}; // end class PhoneNumber
#endif
// Fig. 11.4: PhoneNumber.cpp
// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber.
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
// overloaded stream insertion operator; cannot be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream& operator<<(ostream& output, const PhoneNumber& number)
{
output << "Area Code: " << number.areaCode << "\nExchange: "
<< number.exchange << "\nLine: " << number.line << "\n"
<< "(" << number.areaCode << ") " << number.exchange << "-"
<< number.line << "\n";;
return output; // enables cout << a << b << c;
} // end function operator<<
// overloaded stream extraction operator; cannot be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber;
istream& operator>>(istream& input, PhoneNumber& number)
{
input.ignore(); // skip (
input >> setw(3) >> number.areaCode; // input area code
input.ignore(2); // skip ) and space
input >> setw(3) >> number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >> setw(4) >> number.line; // input line
return input; // enables cin >> a >> b >> c;
} // end function operator>>
// Fig. 11.5: fig11_05.cpp
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators.
#include <iostream>
#include "PhoneNumber.h"
using namespace std;
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the global function call operator>>( cin, phone )
cin >> phone;
cout << "\nThe phone number entered was:\n";
// cout << phone invokes operator<< by implicitly issuing
// the global function call operator<<( cout, phone )
cout << phone << endl;
} // end main
Note: I recently installed then uninstalled CUDA toolkit 8. I needed a newer version of Xcode, so I installed Xcode the newest version 8.2.1, and kept the old version in a different directory just in case. I don't think the problem is with the installation of Xcode though. Also when I installed CUDA I had to set DYLD_LIBRARY_PATH to a directory. However, it might not be the source of the problem! I am just trying to help you figure out the problem to help me fix it :)
Thank you in advance! You are such a good community!
Your attempt to build the program:
g++ Fig11_05.cpp -o Fig11_05
is unsuccessful because you have neglected to compile and link into the
program the code that contains the definitions of the functions:
std::ostream& operator<<(std::ostream& output, const PhoneNumber& number)
and:
std::istream& operator>>(std::istream& input, PhoneNumber& number)
which are called by your program. That is why the linker says:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from: _main in Fig11_05-1f04bd.o
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from: _main in Fig11_05-1f04bd.o
...
Do this instead:
g++ Fig11_05.cpp PhoneNumber.cpp -o Fig11_05
Or to spell out the process in full:-
Compile source file Fig11_05.cpp to object file Fig11_05.o:
$ g++ -o Fig11_05.o -c Fig11_05.cpp
Compile source file PhoneNumber.cpp to object file PhoneNumber.o:
$ g++ -o PhoneNumber.o -c PhoneNumber.cpp
Link the object files Fig11_05.o and PhoneNumber.o into program Fig11_05
$ g++ -o Fig11_05 Fig11_05.o PhoneNumber.o
You can then run:
./Fig11_05
Here is a fairly good beginner's tutorial
about building C or C++ programs with GCC.

gtkmm undefined reference to certain gtk::builder function add_from_file

I am using eclipse, mingw-w64, gtkmm2.4, glade to compile some simple program.
I can compile hello world gtkmm examples, following a tutorial to, however when it comes to glade came a little strange undefined to error.
program that compiled and run smoothly, which was the gtkmm 2.24 simple example tutorial https://developer.gnome.org/gtkmm-tutorial/2.24/sec-basics-simple-example.html.en
#include <gtkmm.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
Gtk::Window window;
Gtk::Main::run(*window);
return 0;
}
however when I try to run another simple example from the glade chapter (24.2.1) things does not work out.
https://developer.gnome.org/gtkmm-tutorial/2.24/sec-builder-accessing-widgets.html.en
example:
#include <gtkmm.h>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
//Gtk::Window window; //I changed it to fit with the glade example
Gtk::Window* window; //I changed this line from the example
//////////////// this part was pretty much just copied out from the example//////
Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
try
{
refBuilder->add_from_file("something.glade"); //this is the line with problem.
}
catch(const Glib::FileError& ex)
{
std::cerr << "FileError: " << ex.what() << std::endl;
return 1;
}
catch(const Gtk::BuilderError& ex)
{
std::cerr << "BuilderError: " << ex.what() << std::endl;
return 1;
}
refBuilder->get_widget("window1", window);
//////////////// end of copied out from the example//////
Gtk::Main::run(*window);
return 0;
}
when compiled, it gave error as follow
test.cpp:(.text.startup+0x281): undefined reference to Gtk::Builder::add_from_file(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
It seems to take the argument "something.glade" as type std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const& (which I have no idea what it is).
according to the gtkmm manual (https:developer.gnome.org/gtkmm/stable/classGtk_1_1Builder.html#aa3f4af4e7eaf7861c8283dc0dbd5254c) seems it only takes Gtk::Builder::add_from_file(const std::string & filename). So is the argument type really the problem?
I have tried casting it as std::string by doing std::string() or string(), but it gave the same error.
I have tried commenting out the line in question, and it compiled fine.
Gtk::Builder::create() did not receive undefined reference to compilation error
refBuilder->get_widget("window1", window); did not receive undefined reference to compilation error
So now I am scratching my head all over this seems trivial issue. Please provide some help.
For more information
pkg-config --cflags --libs yielded -IE:/gtkmm64/include/gtkmm-2.4 -IE:/gtkmm64/lib/gtkmm-2.4/include -IE:/gtkmm64/include/atkmm-1.6 -IE:/gtkmm64/include/giomm-2.4 -IE:/gtkmm64/lib/giomm-2.4/include -IE:/gtkmm64/include/pangomm-1.4 -IE:/gtkmm64/lib/pangomm-1.4/include -IE:/gtkmm64/include/gtk-2.0 -IE:/gtkmm64/include/gdkmm-2.4 -IE:/gtkmm64/lib/gdkmm-2.4/include -IE:/gtkmm64/include/atk-1.0 -IE:/gtkmm64/include/glibmm-2.4 -IE:/gtkmm64/lib/glibmm-2.4/include -IE:/gtkmm64/include/glib-2.0 -IE:/gtkmm64/lib/glib-2.0/include -IE:/gtkmm64/include/sigc++-2.0 -IE:/gtkmm64/lib/sigc++-2.0/include -IE:/gtkmm64/include/cairomm-1.0 -IE:/gtkmm64/lib/cairomm-1.0/include -IE:/gtkmm64/include/pango-1.0 -IE:/gtkmm64/include/cairo -IE:/gtkmm64/include -IE:/gtkmm64/include/freetype2 -IE:/gtkmm64/include/libpng14 -IE:/gtkmm64/lib/gtk-2.0/include -IE:/gtkmm64/include/gdk-pixbuf-2.0 -LE:/gtkmm64/lib -lgtkmm-2.4 -latkmm-1.6 -lgdkmm-2.4 -lgiomm-2.4 -lpangomm-1.4 -lgtk-win32-2.0 -lglibmm-2.4 -lcairomm-1.0 -lsigc-2.0 -lgdk-win32-2.0 -latk-1.0 -lgio-2.0 -lpangowin32-1.0 -lgdi32 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lpng14 -lpango-1.0 -lcairo -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl, Which I have included into the project
pkg-config --modversion gtkmm2.4 yielded 2.22.0, so I doubt it has anything to do with the 2.14 requirement for add_from_file()
pkg-config --modversion gtk+2.0 yielded 2.22.0
I did not use any flags like --std=c++xx.
windows 8 64bit
seems Converting std::__cxx11::string to std::string solved it.
put #define _GLIBCXX_USE_CXX11_ABI 0 at start because I am using gcc 5.

std::scoped_allocator_adapator code fails to compile with vector of strings

In the hopes of using it for my needs I'm playing with std::scoped_allocator_adaptor. This minimal example uses strings and vector, but it fails to compile with on both clang++ and g++.
#include <string>
#include <iostream>
#include <vector>
#include <scoped_allocator>
struct SomeStruct
{
template <typename T>
using Allocator = std::allocator<T>;
template <typename T>
using ScopedAllocator = std::scoped_allocator_adaptor<Allocator<T>>;
using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>;
template <typename T>
using Vector = std::vector<T, ScopedAllocator<T>>;
SomeStruct()
: s{allocator},
v(ScopedAllocator<String>{allocator})
{
}
std::allocator<char> allocator;
String s;
Vector<String> v;
};
int main(int argc, char* argv[])
{
SomeStruct ss;
std::cout << "faffing with vector\n";
ss.v.push_back("hello"); // *** This fails to compile.
std::cout << ss.v[0] << "\n";
return 0;
}
In reality std::allocator won't be used, it's just a placeholder. This attempt was influenced by http://www.stroustrup.com/C++11FAQ.html#scoped-allocator
The compiler error message is lengthy, but boils down to the fact that a suitable constructor cannot be found.
Ubuntu clang version 3.4-1ubuntu3 (tags/RELEASE_34/final) (based on LLVM 3.4)
g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
and with C++14 at ideone.com

Linking libraries OpenCV 2.4.2 on xcode 4.5.1

I have installed opencv with macports following the directions here: Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode
I have also search and tried every other variation of this on stackexchange and google, but this seems to get me closest.
It seems to work for some things, but not for sample code that ships with 2.4.2. Note that I have added ALL opencv 2.4.2 dylibs Link Binary with Libraries.
For example, the following will compile and run:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main ( int argc, char **argv )
{
cvNamedWindow( "My Window", 1 );
IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
CvFont font;
double hScale = 1.0;
double vScale = 1.0;
int lineWidth = 1;
cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
hScale, vScale, 0, lineWidth );
cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
cvScalar( 255, 255, 0 ) );
cvShowImage( "My Window", img );
cvWaitKey();
return 0;
}
However, when I try to build any of the samples, such as the display_image.cpp, example, as follows, I get link errors.
-DOES NOT WORK-
#include <stdio.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/flann/miniflann.hpp"
using namespace cv; // all the new API is put into "cv" namespace. Export its content
using namespace std;
using namespace cv::flann;
static void help()
{
cout <<
"\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
"It shows reading of images, converting to planes and merging back, color conversion\n"
"and also iterating through pixels.\n"
"Call:\n"
"./image [image-name Default: lena.jpg]\n" << endl;
}
int main(int argc, char *argv[])
{
help();
const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
if(img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
if( !img.data ) // check if the image has been loaded properly
return -1;
Mat img_yuv;
cvtColor(img, img_yuv, CV_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically
vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
split(img_yuv, planes); // split the image into separate color planes
imshow("image with grain", img);
waitKey();
return 0;
}
I get the following errors:
Undefined symbols for architecture x86_64:
"cv::split(cv::Mat const&, std::__1::vector<cv::Mat, std::__1::allocator<cv::Mat> >&)", referenced from:
_main in main1.o
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
_main in main1.o
"cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
_main in main1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea how to resolve this?
I had the same problem. A build setting default seems to be different in Xcode 4.5.
Under "Build Settings"--> Apple LLVM compiler 4.1 - Language >
C++ Standard Library:=
Change from libc++ (LLVM ...) to libstdc++ (GNU C++ ...).
It's very likely that OpenCV has not been compiled with C++11 settings, while the program is.
Set the build of your tool without C++11 switches (i.e. -std=c++11 -stdlib=libc++).
Try to manually add the directory where port puts all the dylibs (/opt/local/lib if I'm not getting wrong) in Build Settings->Library search path. This should fix the linking problem.

Resources