How to link boost thread with Xcode - c++

I have built Boost from the website using
./bootstrap.sh
./b2 install
I think all are installed properly. I have headers in /usr/local/include/boost and libs in /usr/local/lib.
Everything links as long as don't include boost/thread.hpp:
//
// main.cpp
// ising3
//
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>
#include <iostream>
#include <boost/date_time.hpp>
//#include <boost/thread.hpp>
using namespace ::boost::tuples;
using namespace ::boost;
int main(int argc, const char * argv[])
{
// insert code here...
std::cout << "Hello, World!\n";
tuple<int,int> a,b,c;
a=make_tuple(1,1);
b=make_tuple(3,2);
std::cout<<a;
return 0;
}
Runs and prints:
Hello, World!
(1 1)
However, if uncommented, it fails:
I have linked the dynamic library libboost_thread.a and libboost_thread.dylib and included /usr/local/include into header search path and /usr/local/lib into library search path.

You should also link to boost_system.
The error indicates that it needs boost::system::system_category (which exists for error reporting).

Related

C++ Use shared library in other project without specifying -I

I have built a shared object to later use a function DoSomethingUseful() from the shared object in other projects. It uses external libraries as well as a bunch of headers that I am using across multiple projects.
Using CMake, I created a project MySharedLib with a header file called library.h:
#ifndef MYSHAREDLIB_LIBRARY_H
#define MYSHAREDLIB_LIBRARY_H
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <cstdio>
// own header files
#include <header1.h>
#include <header2.h>
#define PI 3.14159265
//tesseract
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
//openCV
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
//face detection
#include "opencv2/objdetect.hpp"
#include "opencv2/imgproc.hpp"
void DoSomethingUseul(int[] inparray);
#endif
With library.cpp as
#include "library.h"
void DoSomethingUseful(int[] inparray){...}
My CMake file is as such:
cmake_minimum_required(VERSION 3.10)
project(MYSHAREDLIB)
find_package( OpenCV REQUIRED )
set(CMAKE_CXX_STANDARD 11)
set(MY_INCLUDE_DIR ../source/)
set(MY_OPENCV_CASCADES /opencvpath/openCV34/opencv/sources/data/haarcascades/)
include_directories(${MY_INCLUDE_DIR} ${MY_OPENCV_CASCADES} /usr/include)
link_directories(${MY_INCLUDE_DIR})
add_library(MYSHAREDLIB SHARED library.cpp library.h
${MY_INCLUDE_DIR}header1.cpp
${MY_INCLUDE_DIR}header1.h
${MY_INCLUDE_DIR}header2.cpp
${MY_INCLUDE_DIR}header2.h
)
set_target_properties(MYSHAREDLIB PROPERTIES VERSION 3.10)
target_link_libraries(MYSHAREDLIB lept tesseract ${OpenCV_LIBS})
The *.so file is created sucessfully, i. e. using Clion, no errors are thrown and the file libMySharedLib.so is there.
I then want to use the function DoSomethingUseful() in another file DoSomething.cpp:
#include <iostream>
#include "library.h"
using namespace std;
int main()
{
int[2] myarray; myarray[0]=1; myarray[1] =2;
DoSomethingUseful(myarray);
return 0;
}
And
g++ -g -Wall -Wl,-rpath -Wl,'pwd' -o DoSomething DoSomething.cpp -I ../source/ -L. libMYSHAREDLIB.so
I can then call the programm by ./DoSomething just properly.
The problem is, that this is not what I want. I want to compile a new project file such as ./DoSomethingOnlyDependentOnMYSHAREDLIB. Although I already link header1.h and header2.h in MySharedLib.so in the Cmake file:
add_library(MYSHAREDLIB SHARED library.cpp library.h
${MY_INCLUDE_DIR}header1.cpp
${MY_INCLUDE_DIR}header1.h
${MY_INCLUDE_DIR}header2.cpp
${MY_INCLUDE_DIR}header2.h
)
I do still need the information of the directive of header1.h and header2.hduring linking and compiling by
-I ../source/
How can I avoid this? I only want to copy my *.so somewhere and create new function calls in new *.cpp's only based on MySharedLib.so (i. e. only dependent on one file).
Thanks so much for your help!

undefined reference to `boost::filesystem::path::codecvt() CodeBlocks Windows

I'm trying to use boost on CodeBlocks. I have codeblocks configured with MinGW-w64. I alredy have included the following libraries in the linker settings
libboost_system-mgw73-mt-d-x32-1_66.a
libboost_system-mgw73-mt-x64-1_66.a
libboost_filesystem-mgw73-mt-x64-1_66.dll.a
I also try changing the order...
This is the source code
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
int main(int argc, char* argv[])
{
if( fs::is_directory("."))
cout << "Works!" << endl;
return 0;
}
i have read many "solutions" but they didn't work.
i tried:
changing the #include for this
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
creating a new compiler flag with boths values
-lboost_filesystem-mt
and also
-lboost_filesystem
but i always get the same error
...\boost_1_66_0\boost\filesystem\path.hpp|981|undefined reference to `boost::filesystem::path::codecvt()'|

compile c++ boost test program on command line

I've registered an account at exercism.io and is working on the c++ test case. Trying to wrap my head around boost test I created this simple bob.cpp program:
#include "bob.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char const *argv[]) {
string s = bob::hey("Claus");
cout << s << endl;
return 0;
}
bob.h:
#include <string>
namespace bob {
std::string hey(std::string s) {
return "Hello " + s;
}
}
Compiling in terminal with 'clang++ bob.cpp' and running with ./a.out works. Wrote a boost test using this link: c++ Using boost test
bob_test.cpp:
#include "bob.h"
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(greeting) {
BOOST_CHECK_EQUAL("Hello Claus", bob::hey("Claus"));
}
But when I try to compile it using
~/devel/cpp/boost%>clang++ -I /opt/local/include -l boost_unit_test_framework bob_test.cpp
ld: library not found for -lboost_unit_test_framework
clang: error: linker command failed with exit code 1 (use -v to see invocation)
regards
Claus
This is on Yosemite with Xcode 6.0.1, boost 1.56 installed via macports. Tried on a Mavericks with same Xcode and boost 1.55 but same result.
I got it working by changing the parameters passed to the linker:
clang++ -I /opt/local/include -Wl,/opt/local/lib/libboost_unit_test_framework.a bob_test.cpp
^^^^
and provide the complete path to the library.
And to enable c++11 features add this:
-std=c++11
You forgot the library path:
$ clang++ -I /opt/local/include -L /opt/local/lib -l boost_unit_test_framework bob_test.cpp
^^^^^^^^^^^^^^^^^
The link error you get after fixing this indicates that you have no main() function - it seems that the boost unit test framework will generate this for you provided you have all the necessary boilerplate in place - see http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/test-organization/auto-test-suite.html for details, but it looks like you may need:
#define BOOST_AUTO_TEST_MAIN
#include <boost/test/auto_unit_test.hpp>
rather than:
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

MinGW include paths conflict

I'm trying to cross-compile on Linux for Win64 using MinGW-w64. Here's my code
#include <cstdlib>
#include <iostream>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
int main(int argc, char** argv)
{
if (argc > 1)
std::cout << std::atoi(argv[1]) << std::endl;
std::cout << boost::uuids::random_generator()() << std::endl;
return 0;
}
A simple compile fails with the error
$ x86_64-w64-mingw32-c++ hello.cpp
hello.cpp:4:31: fatal error: boost/uuid/uuid.hpp: No such file or directory
#include <boost/uuid/uuid.hpp>
^
Boost is installed in /usr/include which apparently the MinGW compiler doesn't search. If I add that path, then...
$ x86_64-w64-mingw32-c++ -I/usr/include hello.cpp
In file included from /usr/include/stdlib.h:314:0,
from /usr/x86_64-w64-mingw32/include/c++/4.9.1/cstdlib:72,
from hello.cpp:1:
/usr/include/sys/types.h:109:19: error: conflicting declaration ‘typedef __ssize_t ssize_t’
typedef __ssize_t ssize_t;
^
MinGW's cstdlib is including /usr/include/stdlib.h instead of /usr/x86_64-w64-mingw32/include/stdlib.h! How do I solve this? I need the -I in order to include Boost, but then MinGW includes other headers incorrectly.
You cannot use boost headers from /usr/include to cross-compile windows binaries.
You should also cross-compile boost.
See this guide for details about how to cross-compile boost on linux (it's for vle, but the first part is about boost):
http://www.vle-project.org/wiki/Cross_compilation_Win32
Update: Given that the guide is a little bit old, makes sense to link to boost documentation:
http://www.boost.org/boost-build2/doc/html/bbv2/tasks/crosscompile.html

Boost precompiled headers problem

I try to precompile Boost headers.
First experiment - with std:: headers. I create file std.hpp:
#include <vector>
#include <iostream>
// And other std:: headers
After that:
g++ std.hpp
Copy std.hpp.gch in /usr/include/c++/4.4.5
And write test program:
#include <std.hpp>
int main() {
std::cout << "Hello, precompiled world!" << std::endl;
return 0;
}
Works fine.
Now try precompile Boost headers.
I create boost.hpp file:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/karma.hpp>
After that:
g++ boost.hpp
Copy boost.hpp.gch in /usr/local/include/boost
And write test program:
#include <boost/boost.hpp>
int main() {
// Some code...
return 0;
}
But got error:
main.cpp:2:33: error: /usr/local/include/boost/boost.hpp: No such file or directory.
Try, for experiment:
#include </usr/local/include/boost/boost.hpp>
int main() {
// Some code...
return 0;
}
Same error.
Try copy boost.hpp.gch in another place - same error.
If I put file boost.hpp in same place - works fine (so there is no problems with path):
ls /usr/local/include/boost | grep boost
boost.hpp
boost.hpp.gch
So compiler use boost.hpp header. But why compiler don't see precompiled boost.hpp.gch??
This might be a gcc bug as documented in http://gcc.gnu.org/bugzilla/show_bug.cgi?id=46110