How to include boost::thread in your C++ project? - c++

What do I need to do to include boost::thread in my project? I have copied the whole thread folder to my working path (I wish to be able to run this on several computers) and I get
fatal error C1083: Cannot open include
file:
'boost/thread/detail/platform.hpp': No
such file or directory
From the line #include "thread/thread.hpp"
What gives?
edit:
Even if I just link to the boost folder where the precompiled binary installed and I use #include <boost/thread/thread.hpp> I get
fatal error LNK1104: cannot open file
'libboost_thread-vc90-mt-1_41.lib'

Unfortunately boost::thread is not a "header-only" library -- hence you need to have it compiled. There are basically two ways to go around it.
you download a prebuilt install package from boostpro (assuming that you are on windows) -- https://sourceforge.net/projects/boost/files/boost-binaries/
you can build it yourself - see http://www.boost.org/doc/libs/1_35_0/more/getting_started/index.html

Once you have downloaded, unzipped and installed the boost libraries in your Visual Studio environment, and told the Visual Studio project where the Boost libraries live, you are not quite finished yet. There exist a number of libraries in Boost libraries that require that you build them yourself. Boost threads is one such library.
Build the bjam.exe program if you have not already done it. Probably the simplest way to is to get and run it direct from BoostPro, telling the installation which of the libraries (threads) you wish to install – you don't have to install all of them.
Go to the C:\Program Files\boost_1_46_1\tools\build\v2\engine\src directory and run build.bat from the command prompt. Running the build.bat script will create bjam.exe inside this directory:
C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86
Select the bjam.exe into in your PATH environment variables. Include the directory C:\Program Files\boost_1_46_1\tools\build\v2\engine\src\bin.ntx86 as another environment variable.
At the command prompt, go to the C:\Program Files\boost_1_46_1 directory, enter “bjam”, waiting for approximately 5-10 minutes while the program gets created.
In your Visual Studio project select Configuration Properties -> Linker -> Input -> Additional Dependencies and enter libboost_thread-vc100-mt-gd-1_46_1.lib.
In your Visual Studio project set the project configuration properties -> Linker -> General -> Additional Include Directories, telling it the location of the stage/lib folder eg C:\Program Files\Boost_1_46_1\stage\lib.
That should be sufficient to get you going. For more comprehensive details, please see this blog posting.

I was getting compile time error for 'boost::thread'. But it is resolved when I included following header.
#include <boost\thread.hpp>

Fatal Error C1083 is a Visual C++ error. You should include the library folder from boost in your project. "C:\Program Files\boost\boost_1_41\lib" if you're using boostpro.
Also when you're downloading the thread library with boostpro, you need to check it in the list (you can also choose the compiler...).

Related

Why does the C++ compiler look in "c:\program files\gnu emacs\include" for include files?

I'm trying to compile curl from source (the zip file) in Windows 10 in order to get the curl library (which in turn is needed to build osgEarth). However, when I have used CMake to generated the Visual Studio projects and I try to compile the ALL_BUILD project using the "default native compiler" (according to CMake), I'm getting a lot of errors, of which the first error says
c:\program files\gnu emacs\include\zconf.h(475): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory
Why does the compiler read c:\program files\gnu emacs\include\zconf.h?
To me, it doesn't make any sense that it searches c:\program files\gnu emacs\include for include files, since the compilation should be independent of whether emacs is installed (and if it hadn't been installed, this file wouldn't exist). I have searched through all environment variables to se if any of them could be telling the compiler to look in c:\program files\gnu emacs\include for include files, but the only environment variables the term emacs exists in (case insensitive search) is PATH and ORIGINAL_PATH (which I don't know what it does).
Is it perhaps something in the CMake project that makes the compiler search this folder?
CMake tries to locate your local zlib install path. There are different possible locations, for more details check FindZLIB.cmake somehwere in your cmake install path. My local version will search in $ZLIB_ROOT or some windows registry key, maybe one of these variables have been set on your system when installing emacs.

Boost Library cannot get to work in C++, Include directories not working

I recently tried to install boost libraries in C++ 14, and I added it's include paths like:
Solution Explorer > Project Name > Property Pages > VC++ Directories > "C:\Program Files (x86)\Microsoft Visual Studio 14.0\boost\boost"
I tried to compile after adding this:
#include <boost\variant.hpp>
In Error List window, I can see E1696 - cannot open source file "boost\variant.hpp" and I can't compile like before.
Then I tried with adding a backslash like "C:\Program Files (x86)\Microsoft Visual Studio 14.0\boost\boost\", still didn't work.
I also read this post and explicitly specified it's directory, but even didn't work.
Again, I read this post and did exactly same what is in given answer (as I already built project several times) , but still no success.
However, if I include a library like:
#include "C:\Program Files (x86)\Microsoft Visual Studio 14.0\boost\boost\variant.hpp"
Now compiler recognizes it, but now I can see more than 100 errors in Error List window, those errors are pointed to header files of boost libraries, not in my project file which has variant.hpp included.
All those errors are E1696 - cannot open source file "boost\<libraryname.hpp>" or E1696 - cannot open source file "boost\<subdirs>\<some other files included in libraryname.hpp>"
So, if I remove the line #include "C:\Program Files (x86)\Microsoft Visual Studio 14.0\boost\boost\variant.hpp" from my project's header file, all errors disappear suddenly and project compiles fine! no any single error now!
I want boost to work anyway, so I can use it in projects, but I can't manually edit all those header files and change <boost\... to original locations.
Please help me to get rid of this issue.
make sure you download and install the correct boost version. Installing it in the visual studio directories is possible, but not advised. I suggest you use one of the packages from here. Assuming you use visual studio 2017 and you are developing for 64bit, this could perhaps be the correct package for you.
make sure you do both: adding the include search path and the library search path to your visual studio.
The include search path should point to the boost-installation root directory (the one that contains the Jamroot file and a boost subdirectory). The library search path should point to the correct library subfolder within the boost installation. This is one of the subfolders that start with lib64-msvc-**.* (or lib32-msvc-* if you're developing for 32bit).
The default install path of the binary boost package above will install it into C:\local\boost_<boost version>. Make sure you use the paths from this installation directory and follow the instructions here.
Example:
Include search path: C:\local\boost_1_64_0
Library search path: C:\local\boost_1_64_0\lib64-msvc-14.1

Getting LibCurl to work with Visual Studio 2013

I am having trouble getting LibCurl to work with Visual Studio 2013. I downloaded the current version (curl-7.33.0) and tried following the instructions I found on this site: Using LibCurl with Visual 2010
But I can't find curllib.lib in the folder I downloaded. And I am still getting errors:
After searching the internet for more help. I now get these error messages. There appears to be a problem with linking to libcurl.lib?
This is what I have configured:
Inside /lib I have libcurl.lib and libcurl.dll
UPDATE
I downloaded this release for Win32 MSVC: http://curl.haxx.se/download.html#Win32
After adding the libcurl libraries and successfully compiling, I am now getting this error message:
The application was unable to start correctly (0xc000007b). Click OK to close the application.
Here is the sample code I am trying to run:
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
FINAL UPDATE
I believe I have gotten LibCurl to work with Visual Studio 2013 now. Persistence ftw!
Although, after spending hours trying to solve these error messages, I am a little hesitant at saying everything is working fine now. That is why I am putting a bounty on this question to get clear and concise instructions on getting LibCurl to work with Visual Studio 2013.
This is what I did to get it to work:
First, download the Win32 MSVC package here: http://curl.haxx.se/download.html#Win32
For these instructions sake, let's say you downloaded to C:\LibCurl
Start a new project in Visual Studio. Go to Project|Project Properties|VC++ Directories|Include Directories|
Add the path to the include directory inside the downloaded package. (C:\LibCurl\include)
Next, go to Project|Project Properties|Linker|General|Additional Library Directories|
Add the path to the lib directory. (Where curllib.dll is located)
Then, go to Project|Project Properties|Linker|Input|Additional Dependencies|
And add curllib.lib
Now if you compile a test program, you will likely get the message saying libsasl.dll is missing. You will need to download this file and put it in the same directory as your build.
I used 7-Zip to extract libsasl.dll from OpenLDAP for Windows. OpenLDAP for Windows
This is the result of my test code from above:
A lot of these instructions are out of date because they recommend the win32-ssl-devel-msvc package for curl, which no longer exists.
The following instructions allow you to build libcurl using only:
Visual Studio 2013
curl generic source tarball (tested on curl 7.44.0).
A. Build libcurl static library
Download the latest curl generic source from: http://curl.haxx.se/latest.cgi?curl=tar.gz
Extract the source to a local directory (we'll be using C:\libcurl)
Open a command prompt
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat" To initialize your VC environment variables (adjust your VS 2013 installation directory as needed)
cd C:\libcurl\winbuild
nmake /f Makefile.vc mode=static VC=12
The build should appear in C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl
B. Link Against libcurl in Visual Studio
In Visual Studio, right click your project in Solution Explorer, then click "Properties"
Configuration Properties > C/C++ > General > Additional Include Directories: add C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\include
Configuration Properties > C/C++ > Preprocessor > Preprocessor Definitions: add CURL_STATICLIB
Configuration Properties > Linker > General > Additional Library Directories: add C:\libcurl\builds\libcurl-vc12-x86-release-static-ipv6-sspi-winssl\lib
Configuration Properties > Linker > Input > Additional Dependencies: add libcurl_a.lib
C. Call libcurl from Your Project
The following sample shows a call to libcurl:
#include "stdafx.h"
#include <curl/curl.h>
void main(int argc, char* argv[])
{
CURL *curl = curl_easy_init();
if (curl) printf("curl_easy_init() succeeded!\n");
else fprintf(stderr, "Error calling curl_easy_init().\n");
}
I would say that in a comment, but I am lacking in points.
You don't have to copy any .dll into your program run catalog.
Go to Project | Properties | Configuration Properties and in line Envrionment write: PATH=$(ExecutablePath)$(LocalDebuggerEnvironment).
From now on, all .dlls from any catalog you mention in Project|Project Properties|VC++ Directories|Binary should be usable without copying them.
The rest is exactly as you written.
The easiest way to do this that I found is first make sure that nuget is installed.
http://www.nuget.org/
Then create your project.
Then go to http://www.nuget.org/packages/curl/ and follow the instructions which is to go the package manager console and type PM> Install-Package curl
If you then look for the packages directory in your project directory, you will find the include files and the library files. Note that there is a version for Visual Studio 110, not 120, but because libcurl is a C library you can use it with Visual Studio 2013. Make sure the include directory and lib directory are specified under the Visual C++ directories in project properties.
Make sure you have the following files as addition input to the linker
libcurl.lib;libeay32.lib;ssleay32.lib;Ws2_32.lib;libssh2.lib;zlib.lib;wldap32.lib;
Another way to use curl/libcurl is build with CMake v2.8.12+ (assuming that git is already installed on your computer)
Open cmd window and change dir to appropriate folder
git clone https://github.com/bagder/curl.git
mkdir msbuild
cd msbuild
cmake ..\curl -G"Visual Studio 12 Win64" -DCMAKE_INSTALL_PREFIX=C:\curl.vc12 -DCURL_STATICLIB=ON
< ... lots of output here ... >
Open generated CURL.sln in Visual studio and build it.
CMake options I use in example
-G selects build generator. In our case Visual Studio 2013 64 bit target
-DCMAKE_INSTALL_PREFIX - provides root folder where targets should be installed
-DCURL_STATICLIB=ON - generates build for static library
After building install target, your will find bin/include/lib folders in C:\curl.vc12
Provide those path to your solution and build your code with curl lib.
I tried to do it from scratch with VS2012 (I don't have 2013) and it works perfectly.
I downloaded version 7.19.3 from http://curl.haxx.se/latest.cgi?curl=win32-ssl-devel-msvc because it's the only available version for VS.
I added the include directory, not the curl directory as he says in the tutorial.
I compiled a small toy project without any problem.
So, I'm not sure what your problem is, but:
Make sure you download the right archive.
Try to put the cURL folder on a path without space.
If you know someone who use VS2012 or older, try your code with the same include and lib and see if it works.
Paste a minimal working example of your code so I can test it.
This is a bit late, but for those who still have problems, this method worked best for me:
Add VS to the system PATH:
For example: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin.
Download current curl-X.zip from http://curl.haxx.se/download.html and unzip.
Open command line at curl-X/winbuild.
Call vcvars32.bat.
Call nmake /f Makefile.vc mode=static VC=12.
Goto curl-X/builds/libcurl-XXX.
There you find the includes and a libcurl_a.lib.
This lib works fine for me.
Remember to define -DCURL_STATICLIB when you compile your code with this lib.
For Visual Studio 2017, the steps in link worked for me. In case the link expires or specifically for those who download the libcurl zip file instead of cloning from GitHub, I will note down the steps here.
Set environment variables with “C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat” x64. If the command is successful, you will see a message that says Environment initialized for 'x64'
Download and extract the compressed libcurl file from download libcurl. I used the .zip file.
cd into winbuild directory inside the extracted libcurl folder.
Run nmake /f Makefile.vc mode=dll MACHINE=x64 to build. For more information on build options, please refer to BUILD.WINDOWS text file in winbuild folder.
Go up one directory level and cd into builds folder to find the compiled files.
All the best!
The problem is that the targets for the default VS2013 platform tools are not set in the NuGet packages. This is why it works in VS2012 but not VS2013. I manually created replacement targets files. Instructions and download:
https://github.com/evoskuil/curl-nuget-targets
Download the curl v7.37.0 source code and use the Visual Studio project files provided.
I've spent the last few weeks polishing my own personal project files, that were based off the original VC6 files, and adding them to the repository.
.dsw / .dsp (VC6), .sln / .vcproj (VC7, VC7.1, VC8 and VC9 as well as .sln / .vcxproj (VC10, VC11 and VC12) files are provided for both DLL and Static Library builds with support for OpenSSL and Windows SSPI / SChannel in both Win32 and x64 configurations.
I found an easy way to get it work in VC++ using the latest package. I basically followed the steps in Using libcurl in Visual Studio. The libcurl and VC++ are very old in the instruction.
First download the ZIP file on download page https://curl.haxx.se/download.html The ZIP package is https://curl.haxx.se/download/curl-7.50.1.zip
Go to projects-> Windows\VC10 (or your version of VC)\lib\libcurl.sln, open the project in VC++.
Build the project in DLL Release. DLL debug doesn't work on my VC++.
Go to build\Win32\VC10\DLL Release, you can find the lib and dll files generated from previous step.
Create a folder new, with include and lib folders. Copy the libcurb.dll and libcurb.lib whatever is in the DLL Release folder to the new\lib. Copy everything in curl-7.50.1\include to new\include folder.
C++ Properties -> Configuration Properties -> VC++ Directories, add new\include to Include Directories, new\lib to Library Directories; add new\lib to Linker -> General -> Additional Library Directories, add libcurl.lib to Linker -> Input -> Additional Dependencies
It seems that I have to put the dll file under the same folder with executable file.
It should work.

use of Log4cxx in visual studio 2012

I tried to build log4cxx for visual 2012 and, after 4 hours of resolving errors, the build was succesful (i'm not sure :)).
As a second step I tried to create a vs project to test logging.
I used this example but it keep tell me that
mainapp.cpp(2): fatal error C1083: Cannot open include file:
'log4cxx\logger.h': No such file or directory
I already specified in the linker the path of directory debug in additional library directories and I added "log4cxx.lib" additional dependencies .
I'm confused and I don't know how to make it work
thanks in advance for your help
You also need to add the directory, where headers for log4cxx are. This is not for linking phase, but for compiler phase.
This can be done with Project Properties -> Compiler -> Preprocessor (I think, I don't have VS opened now)
You don't include .lib. This file is for linking.
You need to add to includes the directory, which contains directory log4cxx, and that (log4cxx) directory must contain logger.h!

Boost C++ Libraries linker error libboost_serialization-vc100-mt-gd-1_47.lib

I'm using Boost C++ Libraries v1.47 to serialize an class (I used the installer and selected everything and for VS2010).
But I get a error from my Visual Studio 2010 linker "Lnk1104 Can not open libboost_serialization-vc100-mt-gd-1_47.lib". I set up the "c:\program files\boost\boost_1_47" for my includes and "c:\program files\boost\boost_1_47\lib" for my libs. I don't know what's going wrong.
Update
I don't know why but if I put the lib files from the boost directory to "c:\program files\Microsoft Visual Studio 10.0\VC\lib" everything work fine. Boost load the lib files
somehow dynamical.
If you cannot find the file on your computer with the windows searcher just ask anybody else who uses boost to give you a copy of that file (libboost_serialization-vc100-mt-gd-1_47.lib).
My friend from work used to have the same trouble after having installed boost, there was just not the file in the boost folder. At the same time I picked up the file successfully so we copied it to the right directory on his computer.
For some reason precompiled libs in the installer are named on format "boost-module-vc100..." and headers specify library name as "libboost_module-vc100...". So you should specify library name in project settings to make it linked correctly.
I solved this issue by compiling all boost libraries by myself. With modern PC it won't take too long to do.
to resolve this problem,
go to properties -> Linker -> General -> Additional Library Directories
Enter the path of boost:
Boost-path\stage\lib