Unable to use libpqxx library with header <pqxx/pqxx> on XCode - c++

I am trying to use libpqxx library.
I have installed the library and it's under the below path on my machine-
/opt/homebrew/Cellar/libpqxx/
I have also updated search paths as below under Build Settings on Xcode -
Header Search Paths - /opt/homebrew/Cellar/libpqxx/**
Library Search Paths - /opt/homebrew/Cellar/libpqxx/**
For more details check below snapshot -
Search Paths
Have updated Linker Flags under build settings as -
Other Linker Flags -> -lpqxx
For more details check below snapshot -
Linker Flags
However when I compile main.cpp file using header -
#include <pqxx/pqxx>
It throws below error, count of errors = 50, reports below error for all namespaces or classes in library libpqxx-
error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
Need advice to overcome this.
Sample Program -
#include <iostream>
#include <pqxx/pqxx>
#include <string>
using namespace std;
int main() {
try {
// Connect to the database
pqxx::connection; c("user=postgres password=xxxx#123 host=localhost port=5432 dbname=postgres target_session_attrs=read-write");
}
catch (const exception &e) {
cerr << e.what() << endl;
return 1;
}
}
Explanation -
I see compiler reported almost 50 errors, i believe for each class or namespace under libpqxx library. I certainly can't add every header individually, I need help in understanding how to make the header alone when used like # include<pqxx/pqxx> to compile successfully
Any inputs on this will be highly appreciated.

Related

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

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.

How to use websocketpp with C++?

I'm pretty new to WebSocket in C++ and trying to use websocketpp library from Github https://github.com/zaphoyd/websocketpp. Since as I said I'm new I still not able to figure out how to use it. I'm trying to run some sample WebSocket program from the same sample repo.
Can someone please tell me in detail how to use it. I have cloned the repo in my local system and compiling this sample file
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
void on_message(websocketpp::connection_hdl hdl, server::message_ptr msg)
{
std::cout << msg->get_payload() << std::endl;
}
int main()
{
server print_server;
print_server.set_message_handler(&on_message);
print_server.init_asio();
print_server.listen(9002);
print_server.start_accept();
print_server.run();
}
throw the error fatal error: websocketpp/config/asio_no_tls.hpp: No such file or directory
#include <websocketpp/config/asio_no_tls.hpp>
moreover seraching the error button it shows includePath error.
Help me out. Thanks alot in advance.
I guess you get this error because you didn't add this header file to include directories and library directories parts of your project.
Right click on project and select Properties.
Common Properties => VC++ Directories
Include Directories => add the directory where you installed websocketpp library.
Library Directories => add the directory where you built websocketpp library.

MinGW G++ not compiling

I'm trying to compile a simple "hello world" C++ program but when I use the g++ command I get the error "undefined reference to `WinMain#16" this happens when I compile in vscode or in terminal, whats happening?
code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<string> msg {"Hello World"};
for (const string& word : msg)
{
cout << word << " ";
}
cout << endl;
}
Your error is not a compiler, but a linker error.
That means it still compiles fine but fails to link.
A header file gets included (if it can be found in the include path the compiler uses to find headers), so this works fine.
But for linking two (object-)files together it has to know which files should be linked. And how should it decide which files to use?
That's the cause to create projects in it (and other IDE's) or to create makefiles.
If you turn on full command line-logging in "Settings -> Compiler and debugger... -> Global compiler settings -> [the_compiler_you_use] -> Other settings (rightmost tab) -> Compiler loggin" you can see which commands are sent to the compiler/linker and where the difference is between a single file and a project with multiple files.
By the way: your book/tutorial should explain the steps needed to compile and link files, what happens if headers are included and how to use prebuild libraries/dlls.

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

How do I include Boost.Interprocess and Boost.DateTime properly?

This is a really basic question because I am a C++ newbie. I want to use the Boost.Interprocess library, but am having trouble building it. I'm trying to follow these instructions, but it's not working for me. Here is what I have:
#define BOOST_DATE_TIME_NO_LIB
#include <boost/interprocess/shared_memory_object.hpp>
#include <iostream>
using namespace std;
int main() {
cout << "Hello, beautiful world!\n";
}
But I get this error:
boost_1_55_0\boost\date_time\gregorian_calendar.hpp(63) : fatal error C1083: Cannot open include file: 'boost/date_time/gregorian_calendar.ipp': No such file or directory
I know Boost is able to load properly, because I can get an example that uses #include <boost/lambda/lambda.hpp> to work just fine. It's just when I try to include the Boost.Interprocess library that I am having trouble. The cause is clearly because it's having trouble including the Boost.DateTime library properly, but according to the documentation (linked above) I should be able to get by without separately compiling Boost.DateTime if I define BOOST_DATE_TIME_NO_LIB, right?
What am I missing here?
You need to add it to the preprocessor
In VS go to - Project >> properties >> C/C++ >> Preprocessor in the 'Preprocessor Definitions' paste BOOST_DATE_TIME_NO_LIB.
You can download boost libraries here: https://www.boost.org/users/download/
After that, you can include them in your projects. Also, you can check this video on how to add boost libraries in eclipse IDE on Ubuntu: https://www.youtube.com/watch?v=gN8zrnWxFeI