Command to compile using libboost_python on ubuntu - c++

I am trying to compile my code using libboost library, by putting #include <boost/python.hpp> in my C++ code. Could somebody please help me the right command to run this, especially to include and link the library. I'm pretty basic in this.
The command used(but not working):
g++ try.cpp -L /usr/lib/libboost_python.so -o try
EDIT:
The tested code:
#include <boost/python.hpp>
#include <iostream>
int main()
{
std::cout << "Yes, it works :-)" << std::endl;
return 0;
}
Error messages:
from try.cpp:1:
/usr/include/boost/python/enum.hpp:31: error: expected ‘;’ before ‘*’ token
/usr/include/boost/python/enum.hpp:32: error: expected ‘;’ before ‘(’ token
/usr/include/boost/python/enum.hpp:33: error: ‘PyObject’ has not been declared
/usr/include/boost/python/enum.hpp:52: error: expected constructor, destructor, or type conversion before ‘*’ token
/usr/include/boost/python/enum.hpp:67: error: ‘void* boost::python::enum_<T>::convertible_from_python’ is not a static member of ‘struct boost::python::enum_<T>’
/usr/include/boost/python/enum.hpp:67: error: template definition of non-template ‘ void* boost::python::enum_<T>::convertible_from_python’
/usr/include/boost/python/enum.hpp:67: error: ‘PyObject’ was not declared in this scope
/usr/include/boost/python/enum.hpp:67: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: variable or field ‘construct’ declared void
/usr/include/boost/python/enum.hpp:80: error: ‘PyObject’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: expected primary-expression before ‘*’ token
/usr/include/boost/python/enum.hpp:80: error: ‘data’ was not declared in this scope
Another thing is that when I compile g++ -Wall thread_one.cpp -o thread_one -lboost_thread, that works in order to use boost_thread library.

Try this:
g++ try.cpp -o try -lboost_python
It'd be good style to also add -W -Wall -Wextra -pedantic to your compiler invocation (so that your next SO question can be more specific :-)). Also, -O2 or -O3 for optimization is probably a very good idea, especially with Boost. Finally, splitting the building up into separate stages makes recompiling faster when you have multiple files:
g++ -c -o try.o try.cpp -W -Wall -Wextra -pedantic -O2
g++ -o try try.o -s -lboost_python

Finally, it is working. The command used is as below:
g++ -I/usr/include/python2.6 try.cpp -o try -lboost_python -lpython2.6

Related

G++ does not seem to recognize -std=c++11

I compile my code with the flag -std=c++11 given, and I get all kinds of errors depicting that I should use the same flag. Also, auto is not recognised being a type.
Makefile:
GCCPATH = /path/gcc/5.3.0
CC = $(GCCPATH)/bin/g++
DARGS = -ggdb #debug arguments
CARGS = -std=c++11 #C arguments
WARGS = -Wall -Wextra #warning arguments
AARGS = $(DARGS) $(CARGS) $(WARGS) #all arguments
GCCLIBPATH = $(GCCPATH)/lib64
LIBS = -l curl
LIBD = -L $(GCCLIBPATH) -Wl,-rpath=$(GCCLIBPATH)
.PHONY: webspider
webspider: ../title/htmlstreamparser.o filesystem.o
$(CC) $(AARGS) -o $# $#.cpp $+ $(LIBS) $(LIBD)
filesystem:
$(CC) $(AARGS) -c $#.cpp
The warnings and errors I get:
warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
error: ‘weblink’ does not name a type
for(auto weblink: weblinks)
Now my question is: What should I do to make g++ recognise this clearly given flag?
I also tried to replace it with -std=c++0x, to no avail.
EDIT:
Full output of make:
g++ -c -o filesystem.o filesystem.cpp
In file included from filesystem.cpp:1:0:
filesystem.hpp:23:36: warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11
std::string dir = getCurrentPath();
^
filesystem.cpp: In member function ‘std::__cxx11::string Filesystem::createMD5(std::__cxx11::string)’:
filesystem.cpp:49:19: warning: range-based ‘for’ loops only available with -std=c++11 or -std=gnu++11
for(long long c: result)
^
filesystem.cpp: In member function ‘void Filesystem::createLinkIndex(std::__cxx11::string, strVec)’:
filesystem.cpp:57:11: error: ‘weblink’ does not name a type
for(auto weblink: weblinks) {
^
filesystem.cpp:61:1: error: expected ‘;’ before ‘}’ token
}
^
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token
filesystem.cpp:61:1: error: expected ‘;’ before ‘}’ token
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token
filesystem.cpp:61:1: error: expected ‘)’ before ‘}’ token
filesystem.cpp:61:1: error: expected primary-expression before ‘}’ token
make: *** [filesystem.o] Error 1
The problem is you do not specify all your dependencies, in particular how to build all your intermediate object files.
So what happens is make makes up its own rules and invisibly sneaks them in while you're not looking.
The way to control these implicit rules is through setting the correct predefined variables:
CXX := $(GCCPATH)/bin/g++ # c++ compiler
CPPFLAGS := -I/path/to/headers # preprocessor flags
CXXFLAGS := -std=c++11 # compiler flags
LDFLAGS := -L/path/to/libs # linker flags
LDLIBS := -lcurl # libraries to link
# etc...
By using the correct predefined variables, rather than making up your own, you can save a lot of work when building a Makefile.
In the end, based on the comments, it was fixed by changing
filesystem:
$(CC) $(AARGS) -c $#.cpp
to
filesystem.o: filesystem.cpp
$(CC) $(AARGS) -c $+
as Makefile did not understand that I was trying to make filesystem.o with the rule filesystem: .... When stating this explicitely, it worked as intended.
Advantage of this method over the answer of Galik is the ability to use own variables, albeit in this case not that much of an advantage since it is a small project.

Code compiles on g++ with warning but gives error for same code on clang3.1(Xcode 4.3.3)

Following line compiles successfully on g++ but gives error on clang::
static_assert(tBits <= sizeof(ULONG)*8, "This is IO method");
g++ warning ::
there are no arguments to 'static_assert' that depend on a template parameter, so a declaration of 'static_assert' must be available
clang error ::
use of undeclared identifier 'static_assert'; did you mean 'static_cast'?
please help me out.
Function declaration from comment:
template < size_t tBits >
HRESULT DoIO( std::bitset< tBits >& bitsetToSerialize ) const
"static_assert" was introduced in C++11 as a language keyword - not a function or a macro.
Both compilers are giving you the "I don't know this function" warnings/errors.
For the compiler to give you "I don't know this function" when you are using "static_assert", the compiler must not be compiling with C++11 support (-std=c++11).
To demonstrate this, I took the following piece of code:
#include <bitset>
template<size_t tBits>
int DoIO(std::bitset<tBits>& /*bitsetToSerialize*/)
{
static_assert(tBits <= sizeof(unsigned long) * 8, "tBits is too big.");
return tBits;
}
Then I compiled it with GCC 4.7.3 and I got the following error:
osmith#olivia64 ~/src $ g++ -o sa.o -c sa.cpp
sa.cpp: In function ‘int DoIO(std::bitset<_Nb>&)’:
sa.cpp:6:78: error: there are no arguments to ‘static_assert’ that depend on a template parameter, so a declaration of ‘static_assert’ must be available [-fpermissive]
sa.cpp:6:78: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
Then I compiled it with C++11 support enabled and it compiled without a problem:
osmith#olivia64 ~/src $ g++ -std=c++11 -o sa.o -c sa.cpp -Wall
osmith#olivia64 ~/src $
So, then I compiled it with Clang
osmith#olivia64 ~/src $ clang++ -o sa.o -c sa.cpp
sa.cpp:6:9: error: use of undeclared identifier 'static_assert'; did you mean 'static_cast'?
static_assert(tBits <= sizeof(unsigned long) * 8, "tBits is too big.");
^
1 error generated.
and finally I compiled it using Clang with C++11 support, where it compiled fine.
osmith#olivia64 ~/src $ clang --version
Ubuntu clang version 3.2-1~exp9ubuntu1 (tags/RELEASE_32/final) (based on LLVM 3.2)
Target: x86_64-pc-linux-gnu
Thread model: posix
osmith#olivia64 ~/src $ clang++ -std=c++11 -o sa.o -c sa.cpp
osmith#olivia64 ~/src $
Just to be sure, let's give the compiler opportunity to help us and turn on "-Wall":
osmith#olivia64 ~/src $ g++ -Wall -o sa.o -c sa.cpp
sa.cpp:6:9: warning: identifier ‘static_assert’ is a keyword in C++11 [-Wc++0x-compat]
sa.cpp: In function ‘int DoIO(std::bitset<_Nb>&)’:
sa.cpp:6:78: error: there are no arguments to ‘static_assert’ that depend on a template parameter, so a declaration of ‘static_assert’ must be available [-fpermissive]
sa.cpp:6:78: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

unique_ptr compile error

I guess this is embarrassing if I told you I cant get this to compile. would you please help me:
#include<memory>
using namespace std;
int main()
{
std::unique_ptr<int> p1(new int(5));
return 0;
}
$ gcc main.cpp
main.cpp: In function ‘int main()’:
main.cpp:6:2: error: ‘unique_ptr’ was not declared in this scope
main.cpp:6:13: error: expected primary-expression before ‘int’
main.cpp:6:13: error: expected ‘;’ before ‘int’
$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
This is just a guess.
Most likely you compiled your program like this (or similarly) :
g++ main.cpp
If you did, then the problem is that g++ uses c++03 as default. To use c++11 features (and std::unique_ptr), you need to use newer version of c++ :
g++ -std=c++11
or
g++ -std=c++14
and I would recommend to use also -Wall -Wextra -pedantic.
If you are using Code::Blocks, go to Settings > Compiler > Global compiler settings > Compiler settings and look for the Have g++ follow the C++11 ISO C++ language standard [-std=c++11] and check it!
(Code::Blocks will add the -std=c++11 for you when compiling)

Problem linking vector class

I have a big project with a bunch of .cc files that have mostly c code and some c++ code. I want to use a vector in this program since I need an unbounded data type that is simple to make 2d (in other words I don't want to use an array or a list).
The problem is that when I run my make after including the vector class I get errors that I didn't get before. The only line that makes any difference is #include <vector>. I did not write the make files (and there are about 4 or 5 of them) but to the best of my knowledge it is using the GNU compiler. I have no vector objects instantiated, already included using namespace std; and the error goes away when I comment out the include directive for vector. On another file in this project however I used the queue library and it didn't have any problem with it. The problem happens when I include it in a .h file, it has no problem with .cc files, but since I will need to instantiate a vector object in the .h file I don't have the commodity of only using .cc files for this. Can someone give me any help for this problem?
Here is the output copied pasted from bash for the part that leads up to the error:
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../threads/main.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../threads
/scheduler.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../threads/synch.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../threads
/system.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../threads
/thread.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../threads
/threadtest.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../machine
/interrupt.cc
g++ -g -Wall -m32 -Wshadow -I../bin -I../filesys -I../userprog -I../threads -I../machine
-DUSER_PROGRAM -DFILESYS_NEEDED -DFILESYS_STUB -DHOST_i386 -DCHANGED -c ../machine
/sysdep.cc
In file included from /usr/include/c++/4.4/vector:61,
from ../threads/system.h:19,
from ../machine/sysdep.cc:77:
/usr/include/c++/4.4/bits/stl_algobase.h:232:56: error: macro "min" passed 3 arguments,
but takes just 2
/usr/include/c++/4.4/bits/stl_algobase.h:253:56: error: macro "max" passed 3 arguments,
but takes just 2
In file included from /usr/include/c++/4.4/vector:61,
from ../threads/system.h:19,
from ../machine/sysdep.cc:77:
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:186: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected unqualified-id before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected ‘)’ before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:209: error: expected initializer before ‘const’
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: ‘std::min’ declared as an ‘inline’
variable
/usr/include/c++/4.4/bits/stl_algobase.h:232: error: template declaration of ‘const _Tp&
std::min’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected primary-expression before
‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:235: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:237: error: expected unqualified-id before
‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: ‘max’ declared as an ‘inline’ variable
/usr/include/c++/4.4/bits/stl_algobase.h:253: error: template declaration of ‘const _Tp&
max’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected primary-expression before
‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:256: error: expected ‘}’ before ‘if’
/usr/include/c++/4.4/bits/stl_algobase.h:258: error: expected unqualified-id before
‘return’
/usr/include/c++/4.4/bits/stl_algobase.h:259: error: expected declaration before ‘}’ token
make[1]: *** [sysdep.o] Error 1
make[1]: Leaving directory `/home/f85/njvanbal/workspace/nachos2/userprog'
make: *** [all] Error 2
Always best to look back to the first error first; in your case:
/usr/include/c++/4.4/bits/stl_algobase.h:232:56: error: macro "min" passed 3 arguments,
but takes just 2
/usr/include/c++/4.4/bits/stl_algobase.h:253:56: error: macro "max" passed 3 arguments,
but takes just 2
Looks like you have defined a macro (or maybe a function) called min and max in your own code. The #include for vector apparently also defines a macro for min and max and the compiler is getting confused. Change the name of your min and max macros.

Make Errors: Missing Includes in C++ Script?

I just got help in how to compile this script a few mintues ago on SO but I have managed to get errors. I am only a beginner in C++ and have no idea what the below erros means or how to fix it.
This is the script in question. I have read the comments from some users suggesting they changed the #include parts but it seems to be exactly what the script has, see this comment.
[root#localhost wkthumb]# qmake-qt4 && make
g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i386 -mtune=generic -fasynchronous-unwind-tables -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -I. -o main.o main.cpp
main.cpp:5:20: error: QWebView: No such file or directory
main.cpp:6:21: error: QWebFrame: No such file or directory
main.cpp:8: error: expected constructor, destructor, or type conversion before ‘*’ token
main.cpp:11: error: ‘QWebView’ has not been declared
main.cpp: In function ‘void loadFinished(bool)’:
main.cpp:18: error: ‘view’ was not declared in this scope
main.cpp:18: error: ‘QWebSettings’ has not been declared
main.cpp:19: error: ‘QWebSettings’ has not been declared
main.cpp:20: error: ‘QWebSettings’ has not been declared
main.cpp: In function ‘int main(int, char**)’:
main.cpp:42: error: ‘view’ was not declared in this scope
main.cpp:42: error: expected type-specifier before ‘QWebView’
main.cpp:42: error: expected `;' before ‘QWebView’
make: *** [main.o] Error 1
I have the web kit on my Fedora Core 10 machine:
qt-4.5.3-9.fc10.i386
qt-devel-4.5.3-9.fc10.i386
Thanks all for any help
The error message indicates that the compiler cannot find what you're trying to include, i.e. <QWebView>. The way to tell the compiler where to look is with the -I flag, to specify include directories (these are not recursive).
Currently, you set the following include dirs:
-I/usr/lib/qt4/mkspecs/linux-g++
-I.
-I/usr/include/QtCore
-I/usr/include/QtGui
-I/usr/include
You need to find where QWebView is located on your system, and add the include path to the commandline (or install QWebView into one of the above dirs).
General note: When you get a lot of errors like this, focus on the first one or two. The later errors (such as ‘QWebView’ has not been declared) will probably be solved by fixing the missing-include error.
main.cpp:18: error: ‘view’ was not declared in this scope
Looks like namespaces have to be provided in the code. Read on namespaces.
main.cpp:20: error: ‘QWebSettings’ has not been declared
No type definition available, can be missing include.