hello world python extension in c++ using boost? - c++

Here's my simple first attempt at a python extension using boost. Can someone help me to understand what's causing the compilation error?
#include <iostream>
using namespace std;
void say_hello(const char* name) {
cout << "Hello " << name << "!\n";
}
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
def("say_hello", say_hello);
}
user#host:~$g++ main.cpp -o test.so
In file included from /usr/include/boost/python/detail/prefix.hpp:13:0, from /usr/include/boost/python/module.hpp:8, from main.cpp:8:
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory compilation terminated.

/usr/include/boost/python/detail/wrap_python.hpp:50:23:
fatal error: pyconfig.h: No such file
or directory compilation terminated.
This line tells exactly why it doesn't work. Your compiler doesn't know where is the pyconfig.h file. You have two options here:
place pyconfig.h in a location that
g++ knows about (i.e. your
project's directory)
add -I DIRECTORY (this is capital i,
not lowercase L) flag to g++ that
will make g++ search DIRECTORY for header files
g++ -I /path/to/my/include/files main.cpp

If you face this problem in your NetBeans then just add "/usr/include/python 2.7/" folder in your NetBeans additional include option. You will get this additional include option in properties.

You need to place pyconfig.h in same directory

Related

gcc can't find libcurl header file in the directory it is located

I am trying to compile a cpp using with the following command:
g++ -IC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include\curl -LC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\lib program.cpp
this program has a header file which uses libcurl. the curl library is at -
C:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include\curl\curl.h
gcc gives the following error even though curl.h is in the path -I
mylibrary.h:26:10: fatal error: curl/curl.h: No such file or directory
#include <curl/curl.h>
what am I doing wrong?
The error message means the file curl/curl.h could not be found in the search path specified by -I. You updated the question with the path to the file so the command should be:
g++ -IC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include -LC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\lib program.cpp
Also, can you please try using \ instead of / as path separator in your mylibrary.h file:
#include <curl\curl.h>

gcc can't find header file

I have this main in a project in c++ but I can't compile it in gcc due to this error:
In file included from main.cpp:11:
../framework/application.h:8:10: fatal error: includes.h: El fitxer o directori no existeix
8 | #include "includes.h"
| ^~~~~~~~~~~~
What it says is that the file "includes.h" doesn't exist. It exist but is in another directory and I don't know how to compile using a header from another directory.
I tried:
gcc -I ../framework/ main.cpp
gcc -I../framework/ main.cpp
gcc -I "../framework/" main.cpp
Testing on my machine with a similar directory structure using the I directive worked for me. Ensure that your path is correct and I would even try using an absolute path as a test.

error: `boost' has not been declared

I'm trying to use boost libraries in my C++ application. I'm trying to compile it using g++ with different options.e.g g++ -I /usr/include/boost/filesystem/ -o test.out test.cpp however it always prompt error: 'boost' has not been declared.
And here is my code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <boost/filesystem.hpp>
using namespace std;
int main (){
string line;
string fileName = "Read.txt";
ifstream file;
string str;
file.open(fileName.c_str());
cout << "Hello, world!\n";
vector<string> fileLines;
fileLines.clear();
while (getline(file, str))
{
fileLines.push_back(line);
}
cout << "Total Line count:"<<fileLines.size()<<endl;
fileLines.clear();
cout << "Total Line count:"<<fileLines.size()<<endl;
boost::filesystem::path p("/tmp/foo.txt");
return 0;
}
I will be glad if you help me to fix this.
P.S. I'm compiling my application in Centos 4.7 and It contains Boost version 1.32 according to /usr/include/boost/version.hpp
Update:
I also commented boost instruction, but there is some problem with includes: boost/filesystem.hpp: No such file or directory.
It sounds like you have not yet installed the boost header files that you need for includes. Since you are on CentOS, you need to:
yum install boost-devel
That will place the header file you want in:
/usr/include/boost/filesystem/path.hpp
Since you are using boost::filesystem::path, you should change your #include <boost/filesystem.hpp> to #include <boost/filesystem/path.hpp>. Since -I /usr/include is passed to gcc by default, you do not need the -I /usr/include/boost/filesystem, unless you changed the include to path.hpp. However, this would be dangerous because another library may have the same header file name and then you may include the wrong header.
You can try:
g++ -std=c++11 -Os -Wall -pedantic test.cpp -lboost_system -lboost_filesystem -o test
I had the same problem
Let me know if is works
best regards,
Accodring to header files in my Centos Linux, I changed
#include <boost/filesystem.hpp>
to
#include <boost/filesystem/path.hpp>
And also compiled my program with special link options:
g++ test.cpp -o test.out -lboost_filesystem

how to compile code using external dll library

I want to compile program with one file test.cpp, which use external library (i have dll and lib). How to do that?
My test.cpp
#include <ExternalLib.h>
#include <iostream>
#include <stdio.h>
#define USE_USB_AUTO_CONNECT 1
int main()
{
printf ("Characters: %c %c \n", 'a', 65);
return 0;
}
ExternalLib is provided by third part company with files: ExternalLib.lib and ExternalLib.dll. There are also files with extension so and a.
When I compile with command:
g++ test.cpp
I get error:
test.cpp:1:20: fatal error: ExternalLib.h: No such file or directory
#include <ExternalLib.h>
Thanks for your help.
I think you are trying to use dll files (compiled in Windows) under Linux, that will not work.
Anyway your error is that the compiler isn't able to locate your header file, to solve this you have several options:
1) Provide the include path when compiling using -I switch
i.e.: g++ test.cpp -I<path to your include directory>
2) Copy the ExternalLib.h to the same directory in which test.cpp lies and use #include "ExternalLib.h" instead of #include <ExternalLib.h>
3) Provide relative path to ExternalLib.h starting from the directory in which test.cpp lies. That is, replace #include <ExternalLib.h> for #include "../../external/ExternalLib.h"
This will solve the No such file or directory problem. Then you will also have to provide the binary/library files using -L (for library path) and -l (for library file) switches.

hypertable library linking problem

i have installed hypertable in /opt/hypertable/current/ and i run an example program from hypertable...
#include <Common/Compat.h>
#include <iostream>
#include <fstream>
#include <string>
#include <Common/System.h>
#include <Common/Error.h>
#include <Hypertable/Lib/Client.h>
#include <Hypertable/Lib/KeySpec.h>
using namespace Hypertable;
int main(int argc, char* argv[]) {
ClientPtr client_ptr;
TablePtr table_ptr;
TableMutatorPtr mutator_ptr;
KeySpec key;
const char* install_dir = "/opt/hypertable/current/";
client_ptr = new Client( System::locate_install_dir(install_dir) );
}
i got this error
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/testes.d" -MT"src/testes.d" -o"src/testes.o" "../src/testes.cpp"
../src/testes.cpp:1: fatal error: Common/Compat.h: No such file or directory
i used eclipse CDT for my development and i linked using project Properties->c/c++build->setting->Libraries->LibrarySetPath(-L) and i have inked the HyperCommon also in -l this i set it as /opt/hypertable/current/include/ can any one tell me y i am getting this error...
There are two different paths you need to set when building software: the include path and the library path. You seem to be confusing them.
The include path is the path to find all the .h files. If you have an include path problem, it will manifest at compile time (when building each individual .o file), which is what you are seeing. "Common/Compat.h: No such file or directory" means you are likely missing an include path.
The library path is the path to find the DLL/shared object files at link time. If you have a library path problem, it will manifest at link time (when creating the final executable from the .o files). You are not up to that stage of compilation.
So doing LibrarySetPath and setting -l or -L is a linker/library thing; you want to fix the include path.
Most likely, you want to add /opt/hypertable/current/include/ to the include path (in Eclipse). On the GCC command line, this will be done with -I /opt/hypertable/current/include/, NOT with -L.
you want to add /opt/hypertable/current/include/ThriftBroker/gen-cpp to the include path
你还得一起编译/opt/hypertable/current/include/ThriftBroker/gen-cpp下的cpp文件