Asio .hpp not found in VSCode, but is included in path - c++

I'm trying to use asio in VSCode with C++. I keep getting the warning:
fatal error: 'asio.hpp' file not found
#include <asio.hpp>
Using the code:
#include <iostream>
#include <asio.hpp>
#include <asio/ts/buffer.hpp>
#include <asio/ts/internet.hpp>
int main(){
asio::error_code ec;
//Create a 'context which is like a platform specific interface
asio::io_context context;
//Get the address of somewhere we wish to connect to
asio::ip::tcp::endpoint endpoint(asio::ip::make_address("93.184.216.34", ec),80);
return 0;
}
In the include path UI settings, I have the following paths listed which should take care of things:
${workspaceFolder}/**
/opt/homebrew/Cellar/asio/**
/opt/homebrew/Cellar/asio/1.24.0_1/include
/opt/homebrew/Cellar/boost/**
/opt/homebrew/Cellar/boost/1.81.0/include/boost
/opt/homebrew/Cellar/boost/1.81.0/include
/Library/Developer/CommandLineTools/SDKs/MacOSX13.1.sdk/usr/include
(there are redundant paths here but I am trying everything at this point)
Finally, it should be able to find the file since the file is there:
ls /opt/homebrew/Cellar/asio/1.24.0_1/include ls /opt/homebrew/Cellar/asio/1.24.0_1/include
Gives:
/opt/homebrew/Cellar/asio/1.24.0_1/include:
asio asio.hpp
I would expect it to just find those files. I have also installed boost using brew. It is available at the boost path included.
I am using Mac M1 with Monterey 12.5.1, and VSCode Version: 1.74.2.
Thanks for the help!

The includePath element in the json file is for intelliSense and not your compiler. If you aren't using CMake you will have to go into tasks.json, there you can specify additional compiler flags.
You want to add a flag: -Ipath/to/asio.

Related

Struggling with dlib, linker errors and save_jpeg in XCode

First, I am new to C++ and dlib but I have successfully built the examples and started working on my own project. Things have been progressing smoothly until I try to save a jpeg. Attempting to compile code using dlib::save_jpeg throws a linker error and I cannot track down the solution. I have attempted to add #define DLIB_JPEG_SUPPORT above and below my #includes but no luck. I am using XCode and used cmake -G "Xcode" .. when I compiled the examples. Relevant code below. Since I am on a Mac, I have added header and library search paths for X11 (for dlib gui), OpenCV, and DLIB. I have libjpeg.dylib and linked that to my project with and without #define DLIB_JPEG_SUPPORT in main.cpp. Is there some other build setting I need to specify? Thank you in advance for your help.
Finally, I have seen other questions and pages about dlib and libjpeg issues but no luck yet. And yes I have source.cpp included in the project.
// the standard stuff
#include <string>
#include <iostream>
#include <unistd.h>
// opencv mat object
#include <opencv2/opencv.hpp>
// dlib>
#include <dlib/opencv.h>
#include <dlib/image_io.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_transforms.h>
int main(int argc, const char * argv[]) {
// retrieving images from a TCP connection
// decode data stream
img = cv::imdecode(rawImage, CV_LOAD_IMAGE_COLOR);
// perform image processing
dlib::cv_image<dlib::bgr_pixel> d_image(img);
// finally save the result to jpg
std::string fname = argv[1] + std::to_string(image_id) + ".jpg";
dlib::save_jpeg(d_image, fname); // <- line that won't compile
return 0;
}
After quit a bit of struggling and side-by-side comparisons I finally found the issue. In XCode go to to Build Settings and modify Other Linker Flags, Run Search Paths, and Other C++ Flags to match the compiled and working face_ex example. I wholesale copied all of those flags and included a missing libjpeg.dylib and was able to get things running. It should look something like this for the C++ flags . Hope this helps the next person.

Clang Tool Cannot Find Required Libraries on Ubuntu 16.10

I am building a simple C++ Clang tool program called ClangEx using CMake on Ubuntu 16.10 x64.
The project has a single main.cpp file. It's contents are as follows:
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
#include "llvm/Support/CommandLine.h"
using namespace clang::tooling;
using namespace llvm;
static llvm::cl::OptionCategory MyToolCategory("my-tool options");
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
static cl::extrahelp MoreHelp("\nMore help text...");
int main(int argc, const char **argv) {
CommonOptionsParser OptionsParser(argc, argv, MyToolCategory);
ClangTool Tool(OptionsParser.getCompilations(),
OptionsParser.getSourcePathList());
return Tool.run(newFrontendActionFactory<clang::SyntaxOnlyAction>().get());
}
It builds successfully with CMake but when I use it to analyze an example C++ program, I get the following error:
$ ./ClangEx SandwichBar.cpp --
In file included from /home/bmuscede/SandwichBar.cpp:11:
In file included from /home/bmuscede/SandwichBar.h:14:
In file included from /home/bmuscede/Customers/Sandwich.h:15:
In file included from /home/bmuscede/Customers/../Capital/Recipe.h:14:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/string:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/bits/char_traits.h:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/bits/postypes.h:40:
In file included from /usr/lib/gcc/x86_64-linux-gnu/6.2.0/../../../../include/c++/6.2.0/cwchar:44:
/usr/include/wchar.h:39:11: fatal error: 'stdarg.h' file not found
# include <stdarg.h>
^
1 error generated.
Error while processing /home/bmuscede/SandwichBar.cpp.
I was able to find this bug but installing clang-3.9 doesn't seem to help my situation.
Any advice would be appreciated.
This problem arises sometimes when you have both gcc and clang installed. By default C_INCLUDE_PATH and CPLUS_INCLUDE_PATH are set to search gcc's own include files and don't include clang's include files. And Clang needs the clang specefic include files. To fix try:
export C_INCLUDE_PATH=$C_INCLUDE_PATH:"<clang_include_path>"
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:"<clang_include_path>"
where <clang_include_path> is usually /usr/lib/clang/<version>/include, but can vary based on your installation. On my system, since I built clang from source it's totally different.
If you permanently want to export the two flags, add the same two lines to ~/.bashrc
Hope that helps.

Compile C++ with static lib

this will probably a dumb question for you guy's but I have no experience in C++ what so ever. I'm using an open source project osrm (which is awesome). Still to request a route, you have make an http request. To reduce the running time, I would like to build a wrapper around the code and call it using the command line. So I googled a bit and found that osrm already creates a static lib (.a file) when compiling the project. I also found a piece of code that points me in the right directions for building a wrapper. So to begin I build a simple hello world program (see below) that includes some files from that static lib. To compile I followed this tutorial.
My directory structure looks like this:
./helloWorld.cpp
./libs/libOSRM.a
And the command to compile is this:
gcc –static helloworld.cpp –L ./libs –l libOSRM.a
The code it selve:
#include "Router.h"
#include "boost/filesystem/path.hpp"
#include "ServerPaths.h"
#include "ProgramOptions.h"
#include <InternalDataFacade.h>
#include <viaroute.hpp>
#include <iostream.h>
main()
{
cout << "Hello World!";
return 0;
}
the exact error I got:
fatal error: ServerPaths.h: No such file or directory #include "ServerPaths.h"
Add the -IPathToTheHeaderFiles to the compiler options. So it will find the files to be included. Replace PathToTheHeaderFiles with the path where your file ServPaths.h resides.
Edit: Add as many -I as you need for further header files.
Additionally it would be worth to read a book about C++ or/and the GCC manual1
1 Section 3.11 will help.

CodeLite not working with boost library

I'm having some troubles to get my CodeLite IDE working with Boost library.
So basically, I've got CodeLite v.6.1.1 and Boost library downloaded from sourceforge. I would love to make it actually working, but I failed to do so using the following concept:
I did create new workspace, than I did go -> Settings -> Build Settings -> Compilers (here I have two compilers, one of which is CodeLite 4.8.1 and the other one is MinGW Code::Blocks, I did select CodeLite 4.8.1. -> Advanced -> Global Paths -> and I did provide two blank lines of Include Path and Libraries Path with the directory of unpacked boost library (Windows 7): C:\boostlib\boost_1_57_0.
In this catalog (i.e.: C:\boostlib\boost_1_57_0), I have got all neccesary files, all of them are unzipped and yet I can't get it working.
Besides, I can't click on apply when I'm done with setting path to files containing boost library, all I can do is click ok but once I'm done with this I can't work with libraries from boost.
The following code isn't working:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
// ERROR MESSAGE: boost/lambda/lambda.hpp, No such file or directory
typedef std::istream_iterator in;
std::for_each(in(std::cin), in(), std::cout << (_1 * 3) << ” ” );
return 0;
}
The message I get is:
boost/lambda/lambda.hpp: No such file or directory
Is there any chance I can get CodeLite working with boost library or should I simply switch IDE and forget about it ?
Your on the right track but need to point to the libs and include folders inside the boost directory.
I use the the http://nuwen.net/mingw.html version of mingw as STL (the guy) includes boost as part of his distribution.
So in codeLite I just have to add
C:\MinGW\lib to the libraries path
C:\MinGW\include to the include path

Trouble linking/compiling Libcurl in Eclipse IDE C++ Project

Hi I am having trouble configuring an Eclipse C++ (32bit) project to use libcurl. I am using MinGW. I am on a 64bit windows OS. I have done the following so far:
Add "libcurl.lib" to the project's Libraries
Add the path of libcurl.lib to the Library Paths
Add the path to the curl header files to the Includes path
In source, I included "curl/curl.h"
However, I am seeing the following message:
Info: Internal Builder is used for build
g++ "-LC:\\projectsrc\\Network\\curl-7.34.0-win32\\lib" "-LC:\\projectsrc\\Network\\curl-7.34.0-win32\\dlls" "-LC:\\projectsrc\\Network\\curl-7.34.0-win32\\bin" -o TestLibCurl.exe "src\\TestLibCurl.o" "-lC:\\projectsrc\\Network\\curl-7.34.0-win32\\lib\\libcurl.lib"
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: cannot find -lC:\projectsrc\Network\curl-7.34.0-win32\lib\libcurl.lib
collect2.exe: error: ld returned 1 exit status
Below is the sample source
#include <stdio.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <string>
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
cout << "test" <<< endl;
return 0;
}
Below is a screenshot of where I added the include path and the library file:
For your reference, I have uploaded my sample C++ eclipse project to dropbox here:
https://www.dropbox.com/s/2jowlkz6qypqe9g/TestingLibCurl.zip
The project file includes main.cpp, the libcurl.lib file, the libcurl header files, and the eclipse project setting files (.cproject, .project, .settings)
Oddly, I am able to get this to work in CodeBlocks IDE, using the same MinGW and using the same computer (Windows 64 bit). Do I need to add linker/options/flags when using Eclipse IDE?