Linker error with Boost Threads in C++ in Xcode - c++

I have been trying to get a simple c++ program running that was given to me as an example of multi-thread programming. I know it runs on another machine with boost installed, but I am trying to run it in Xcode, and this is giving me some problems.
First I tried to run it with my existing version of boost. However I was getting many errors and after doing some research, found that I needed to update my boost version from 1.47 to 1.52 to fix a few known threading bugs contained in 1.47.
That fixed many of my errors, but was still throwing a few. SO I found out that I had to patch 1.52 using the patch found here: https://svn.boost.org/trac/boost/attachment/ticket/7671/libcpp_c11_numeric_limits.patch
After all that, I am still getting a linker error saying "Library not found for -lboost_thread"
Right now in Xcode, I have the following project settings:
Under search paths, I have the header and library search paths set to "usr/local/include/" - - Under linking I have other linker flags set to "-lboost_thread"
I have both a lboost_thread.a and a lboost_thread.dylib located in usr/local/lib/. How do I make xCode find this, as It seems that should fix my problem.
Also if it helps here is the code I am running:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void printNum(char c, int num)
{
for(int i = 0; i < num; i++)
{
cout << c << i << endl;
}
}
int main()
{
cout << "Boost threads!" << endl;
boost::thread t(printNum, 'b', 100);
printNum('a', 100);
cout << "Good bye!" << endl;
}
Image of Error:
Image showing the file in it's location:
Image showing my build settings:

I don't know why the linker isn't finding the library, but I can tell you how to circumvent the issue of library search: Add the full path /usr/local/lib/libboost_thread.a to the Other Linker Flags build setting, without -l or -L.

Your screenshot says /usr/local/lib/libboost_thread.a the advice is to use -I/usr/local/lib/libboost_thread.a maybe that is the thing.

Related

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

Using Boost library in XCode

not particularly good with this kind of stuff I have managed to install the Boost C++ library and this basic program works with the g++ compiler and linking to the right .dylib files.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost;
int main()
{
filesystem::path path1("/Users/username/Documents/testfile.txt");
filesystem::remove(path1);
std::cout << "It worked" << std::endl;
return 0;
}
Now I would like to do the same, just using Xcode to build and run the program, but it doesn't work. I have done everything in this tutorial:
http://buddypanda.com/?p=10
i.e. include header and library search paths, as well as linked the .dylib files of filesystem and system under the 'Build Phases' tab.
The Build succeeds but then the program crashed with an error of EXC_BAD_ACCESS.
I can maybe provide more specifics on what goes wrong if you tell me where to look.
Thanks!
EDIT1: I am using g++-4.9 and Boost-1.58 and Xcode 6.4

No output in c++-program if object is instantiated using Eclipse

I got an issue after installing Eclipse Kepler on my Windows 7 32bit machine. I installed the CDT and the MinGW-compiler. I configured the installation by adding MinGW to the PATH and tested my configuration with a "Hello world"-program, which worked.
The strange thing is, that if I instantiate an object, nothing is outputted. It doesn't matter if it's a std::string or a custom made class. If I instantiate it, nothing is outputted, even if it should be outputted before the instantiation. The exact same code works fine, if I compile it with cygwin gcc from command line. If I change the toolchain to cygwin gcc nothing changes (I've rebuild the program with "build all").
There is no error displayed and no problem listed.
Here's the minimal working example:
#include <iostream>
#include <string>
using namespace std;
class SayWorld{
public:
SayWorld(){
cout << "World!" << endl;
}
};
int main() {
//Only gets outputted, if the lines, that don't work are commented out:
cout << "Hello ";
// Works:
cout << "World!" << endl;
// Doesn't work:
// SayWorld sw;
// Also doesn't work:
// string str("World!");
// cout << str << endl;
return 0;
}
Edit 2:
I narrowed the error to MinGW, as this picture of a Cygwin-Bash-Terminal demonstrates. (The file was not changed beetween the to g++ calls and contains the example above)
Edit 1 (Legacy)
Toolchain-picture:
-picture removed- (don't think it was necessary)
After reinstalling eclipse and MinGW again, step-by-step following this video tutorial:
http://www.youtube.com/watch?v=77xZOT3xer4
and having the same problem afterwards, I stumbled uppon this post in the eclipse forum:
http://www.eclipse.org/forums/index.php/u/104305/
which has brought me to this solution:
Right-click Project -> Properties -> Run/Debug Settings
Choose executable and hit the "New"-button.
Go to the Environment-Tab and create a new variable named PATH with the value: "C:\MinGW\bin".
As I am no expert, I can't explain to you why it works, but it worked for me. If someone knows, how to do this better or wholly avoid this problem, I'd be glad to listen.
This entry was definitively in my Windows-PATH...
PS.: The problem seems also to be known here:
unable to see the output of c++ program in the console

Boost cannot open file, 'libboost_filesystem-vc100-mt-gd-1_47.lib'

I have googled the error for hours on end now and have not gotten much of anywhere. I have linked the project in my Visual Studios (2010 & 2012) project as that seems to have resolved everyone else's issue that was similar to this. However I am still unable to get my sample code from boost's website to work and keep getting that error. The file libboost_filesystem-vc100-mt-gd-1_47.lib is in my C:\Program Files (x86)\boost\boost_1_47\lib path. Here is the code I am trying to test and get boost up and running.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
Enter:
[boost_path]\stage\lib
In setting under Linker->General->Additional Library directories.
As mentionned by SChepurin it could be that you didn't add the path to boost to Linker->Additional Library Directories.
It could also be the case that you are trying to compile for x64 target whereas, given the install path for your boost libraries, you have only the 32 bits boost libraries installed on your system and therefore should either switch to x86 target or get 64 bits boost libraries, You can find binariy release from Boost 1.50 if you can use a moe recent version than 1.47 otherwise you will have to compile them yourself (see boost official documentation for more info on that)
Best
I had the same error and i followed the SourceForge. The link will take you to a folder of zipped lib and dll files for version of boost.You can download and unzip related library and when copied it to the related location everything gonna be alright ;)

How do I get PCRE to work with C++?

This is a newbie question but I hope I can express my question as clearly as possible.
I'm trying to do pattern matching in C++.
I've downloaded the Win32 version of PCRE from here and I've placed the downloaded pcre3.dll and pcreposix3.dll files into the folder of Dev-CPP's lib folder (I'm using Bloodshed Dev-C++ 4.9.9 IDE).
I've also downloaded a pcrecpp.h header file and have it in the same directory I'm writing the following code (not writing actually. I'm coping example code from a PDF tutorial named PCRE- Perl Compatible Regular Express).
But I can't get it to work. The code is as follows:
#include <iostream>
#include <string>
#include <pcrecpp.h>
using namespace std;
int main()
{
int i;
string s;
pcrecpp::RE re("(\\w+):(\\d+)");
if (re.error().length() > 0) {
cout << "PCRE compilation failed with error: " << re.error() << "\n";
}
if (re.PartialMatch("root:1234", &s, &i))
cout << s << " : " << i << "\n";
}
When I compile the code, Dev-C++ gives me a lot of errors including: "`pcrecpp' has not been declared" and "RE" undeclared.
How should I deal with the downloaded files and fix my problem? Or is there something obvious that I'm missing?
If you specify the file for #include with angle brackets (<>), then the compiler will only look for that header in the locations for external libraries, in so far as the compiler is aware of them.
If you instead use quotation marks (""), then the compiler will also look in the locations for the current project, which typically includes the current directory.
The quick fix for your current problem is to use
#include "pcrecpp.h"
The alternative is to tell the compiler where it can find the headers of the PCRE library.
You will have to tell the compiler where it can find the headers of the PCRE library.
How to do this differs from build system to build system, but if you are using an IDE, then there should be an option somewhere to specify the 'Include directories'. This is where you add the directory of the PCRE headers (with full path).
As a side-note: When the compiler gives you a large number of errors and warnings, always start with fixing the first one. I would guess that in this case it was something like "unable to find header: pcrecpp.h".
It is often the case that, if the compiler tries to continue after encountering a problem, more problems are found that are follow-on problems of the first one. When the first problem is fixed, these also magically disappear.
g++ -lpcrecpp ......
you need to add '-lpcrecpp' to g++ command
cout << “PCRE compilation failed with error: “ << re.error() << “\n”;
I just copied your code and tried to compile it. I got the same error as you reported.
The problem is that string you put to cout is not properly started/ended. You should use real " instead of marks which looks like double quotes (") but it is not. If you fix it, your code should compile w/o any error.
You have included
#include <pcrecpp.h>
1st point to check But is file in the inlcude path of your code. Did you download the installable ? Check where it has been installed on your machine.
2nd point is to check do you have the library paths defined, so that they can be resolved during compiling and linking.