Compiling Boost.Python toy example fails: ''error: expected unqualified-id before ‘%’ token %%bp_module" - boost-python

I'm trying to compile a toy example using Boost.Python. I can compile the "quickstart" example.
However when I try to compile this toy example smoke_test.cpp:
%%bp_module smoke_test
#include <boost/python.hpp>
int test() {return 42; }
BOOST_PYTHON_MODULE(smoke_test)
{
boost::python::def("test",test);
}
and smoke_test.py:
import smoke_test
smoke_test.test()
using this Jamfile:
import python ;
import testing ;
project quickstart
: requirements
<location>.
;
# Declare a Python extension called hello.
python-extension extending : smoke_test.cpp ;
I get the following error:
smoke_test.cpp:1:1: error: expected unqualified-id before ‘%’ token
%%bp_module smoke_test
^
Does anyone know whats going wrong? I assume my Jamfile is incorrect but I'm not sure how to fix it.
I'm using boos 1.63.0 and python 2.7.

I guess the %%bp_module smoke_test line was false as mentioned by #doqtor. This was probably a toy example made for ipython notebook only. Although I couldn't figure out how to use bjam and Jamfiles to compile the code, I'm still now able to compile with this minimal Makefile example using my Ubuntu 14.04 system boost version (1.54.0):
PYTHON_VERSION = 2.7
PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
# location of the Boost Python include files and library
BOOST_INC = /usr/include
BOOST_LIB = /usr/lib
# compile mesh classes
TARGET = smoke_test
$(TARGET).so: $(TARGET).o
g++ -shared -Wl,--export-dynamic $(TARGET).o -L$(BOOST_LIB) -lboost_python -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(TARGET).so
$(TARGET).o: $(TARGET).cpp
g++ -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(TARGET).cpp

Related

Need help translating a Make File into Meson Build File for Vala

I am working on an Application in Vala. I need to use both webkit2gtk-web-extension-4.0 and webkit2gtk-4.0 in an single project.
I am following the example from -
https://github.com/rschroll/webkitdom/tree/extension
The Make File from the example is as follows:
PKGS=--pkg gtk+-3.0 --pkg libsoup-2.4
EXEC=dom-test
SOURCES=dom-test.vala custom-web-view.vala
LIB_BASE=dom-server
LIB_SOURCES=$(LIB_BASE).vala
LIBRARY=$(LIB_BASE).so
# If you have webkit2gtk-4.0 installed, you will need a Vala from git
# version 6a1a1ab1 (2014/10/16) or later, in order to have the fixed
# .vapi files.
#WEBKIT_VAPI=--pkg webkit2gtk-4.0
#WEBKIT_EXT_VAPI=--pkg webkit2gtk-web-extension-4.0
# Otherwise, use the -3.0 versions included here
WEBKIT_VAPI=--vapidir=. --pkg webkit2gtk-3.0
WEBKIT_EXT_VAPI=--vapidir=. --pkg webkit2gtk-web-extension-3.0 -X -I/usr/include/webkitgtk-3.0
all: $(EXEC) $(LIBRARY)
$(EXEC): $(SOURCES)
valac $(WEBKIT_VAPI) $(PKGS) $^
$(LIBRARY): $(LIB_SOURCES)
valac $(WEBKIT_EXT_VAPI) $(PKGS) --library=$(LIB_BASE) -X -fPIC -X -shared -o $# $^
clean:
rm $(EXEC) $(LIBRARY)
With Meson Build system, I tried to create a subproject with the shared library and link the shared library in the main project. But I am getting the error
DOMServer.vala:1.1-1.10: error: The root namespace already contains a definition for `WebKit'
The main project meson.build file as follows:
project('com.linappfoundry.athmanam', ['c', 'vala'])
domserver_proj = subproject('DOMServer')
domserver_lib = domserver_proj.get_variable('domserver_lib')
cc = meson.get_compiler('c')
m_dep = cc.find_library('m', required : false)
executable(
meson.project_name(),
'AthmanamApp.vala',
'Widgets/MainWindow.vala',
'Widgets/PoemParserWebView.vala',
dependencies: [
dependency('gtk+-3.0'),
dependency('glib-2.0'),
dependency('libsoup-2.4'),
dependency('webkit2gtk-4.0'),
dependency('json-glib-1.0'),
dependency('libxml-2.0'),
m_dep
],
link_with: [domserver_lib],
install: true
)
And subproject's meson.build file is as follows:
project('com.linappfoundry.athmanam.DOMServer', ['c', 'vala'])
domserver_lib = shared_library(
'com.linappfoundry.athmanam.DOMServer',
'src/DOMServer.vala',
dependencies: [
dependency('glib-2.0'),
dependency('gtk+-3.0'),
dependency('libsoup-2.4'),
dependency('webkit2gtk-web-extension-4.0')
],
install: true
)
I am not sure what I am doing wrong here, as I am new to both Vala and Meson.
The error
DOMServer.vala:1.1-1.10: error: The root namespace already contains a definition for `WebKit'
is from the Vala compiler and is saying there is already a definition of Webkit in the global namespace. The error could be as simple as using a keyword with the wrong case, e.g. Using Webkit; when it should be using Webkit;.
Webkit is defined as a namespace in the global (root) namespace and that is done in the VAPI file. Try and find where it is being redefined as something else.

Error with static compilation Qt with postgresql driver

I have installed through Mainteinance Tool Qt 5.12.5 and the sources. I have the next directories:
C:\Qt\5.12.5\Src
C:\Qt\Tools\mingw730_32\
C:\Qt\Tools\mingw730_64\
On the other hand, I have read that downloable Postgres version is compiled with MSVC, and I must to compile my own version. I have do it following link, and now I have a postgresql version in c:\pgsql
Finally I have added c:\pgsql to user Path
Next step, I have opened PowerShell in Admin mode and I´ve gone to C:\Qt\5.12.5\Src\.
Next, set the env path for this PowerShell session:
$env:Path += ";C:\Qt\Tools\mingw730_64\bin\;C:\Qt\5.12.5\Src;C:\pgsql\include\;C:\pgsql\lib\;C:\pgsql\bin\" (setting the pgsql path again....)
After that, I execute configure.bat like that:
configure -v -static -release -static-runtime -platform win32-g++ -prefix C:\Qt\5.12.5\Estatico\ -opensource -confirm-license -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -opengl desktop -no-openssl -opensource -confirm-license -skip webengine -make libs -nomake tools -nomake examples -nomake tests -sql-psql
But I have get this error:
ERROR: Feature 'sql-psql' was enabled, but the pre-condition 'libs.psql' failed.
Searching in config.log I can read those lines:
loaded result for library config.qtbase_sqldrivers.libraries.psql
Trying source 0 (type pkgConfig) of library psql ...
pkg-config use disabled globally.
=> source produced no result.
Trying source 1 (type psqlConfig) of library psql ...
pg_config not found.
=> source produced no result.
Trying source 2 (type psqlEnv) of library psql ...
None of [liblibpq.dll.a liblibpq.a libpq.dll.a libpq.a libpq.lib] found in [] and global paths.
=> source produced no result.
Trying source 3 (type psqlEnv) of library psql ...
=> source failed condition '!config.win32'.
test config.qtbase_sqldrivers.libraries.psql FAILED
What can I do or what is the properly way to do that?
Thank you in advance.
UPDATE
There are similar question here but it hasn´t been solved, and those question ask about Visual Studio.
I want to compile it under mingw.
The solution suggested by #Soheil Armin doesn´t work too
The solution suggested by #Soheil Armin works fine, but I need to delete the entire source tree and reinstall it as he suggested. If not, a new configure won't work.
Also, the ^ character can be saved:
configure <your parameters>
PSQL_LIBS="C:\pgsql\lib\libpq.a"
-I "C:\pgsql\include"
-L "C:\pgsql\lib"
You need to explicitly define library paths of Postgres.
configure <your parameters> ^
PSQL_LIBS="C:\pgsql\lib\libpq.a" ^
-I "C:\pgsql\include" ^
-L "C:\pgsql\lib"

Swift Test passing arguments

Does anyone know how to provide command-line argument for swift test ?
I have tried :
swift test "myDBName"
But I got unexpected argument error.
List of possible arguments are :
OVERVIEW: Build and run tests
USAGE: swift test [options]
OPTIONS:
--build-path Specify build/cache directory [default: ./.build]
--chdir, -C Change working directory before any other operation
--color Specify color mode (auto|always|never) [default: auto]
--configuration, -c Build with configuration (debug|release) [default: debug]
--enable-prefetching Enable prefetching in resolver
--list-tests, -l Lists test methods in specifier format
--parallel Run the tests in parallel.
--skip-build Skip building the test target
--specifier, -s Run a specific test class or method, Format: <test-module>.<test-case> or <test-module>.<test-case>/<test>
--verbose, -v Increase verbosity of informational output
-Xcc Pass flag through to all C compiler invocations
-Xlinker Pass flag through to all linker invocations
-Xswiftc Pass flag through to all Swift compiler invocations
--help Display available options
Is there any other way to pass args ? ( Environment variable, etc ? )
You can, indeed, use the environment to do it.
A test with the following contents:
final class HelloTests: XCTestCase {
func testExample() {
XCTAssertEqual(String(cString: getenv("SOMETHING")), "else")
}
static var allTests = [
("testExample", testExample),
]
}
Will succeed, using the swift command line:
SOMETHING=else swift test
(This is not explicitly mentioned in OP's question, but swift test refers to the test tool of the Swift Package Manager; likewise does the answer below)
Swift Package Manager Test Tool does not support passing or arguments
If we compare the --help for the Test (swift test) and the Run (swift run) tools, you may note that the USAGE grammar for the former does not allow passing arguments to the binary that the Test Tool wraps, something that is a feature for the latter:
$ swift test --help
OVERVIEW: Build and run tests
USAGE: swift test [options]
...
$ swift run --help
OVERVIEW: Build and run an executable product
USAGE: swift run [options] [executable [arguments ...]]
...
We may verify this if we visit the source code, where the ArgumentParser for the SwiftRunTool binds the command line arguments, whereas the ArgumentParser for SwiftTestTool does not.
If you believe this could be a useful feature, please have a look at the Support page of the Swift Package Manager at GitHub for instructions on how to propose new features / bug fixes.
Using conditional compilation flags
For completeness (in addition to making use of the environment), conditional compilation flags can be used, and possibly suffice, depending on the use case.
This is not providing actual values when launching the tests over the command line, but you may make use of conditional compilation flags (true/false) to the Swift compiler swiftc using the -D flag.
E.g., the following test would pass:
import XCTest
#if USE_LOCALHOST
let server_ip = "127.0.0.1"
#else
let server_ip = "1.2.3.4"
#endif
final class HelloTests: XCTestCase {
func testExample() {
XCTAssertEqual(server_ip, "127.0.0.1")
}
static var allTests = [
("testExample", testExample),
]
}
if the test suite is launched using:
$ swift test -Xswiftc -DUSE_LOCALHOST
Whereas the test would fail if launching using:
$ swift test

How to compile Objective C code in ubuntu

I am beginner in Iphone programming. I am trying to compile (ubuntu).
#import <Foundation/Foundation.h>
int main (void)
{
NSLog (#"Executing");
return 0;
}
I compiled it but getting following error
subhash#subhash-Lenovo-G570:~/grit/iphone/mac$ gcc -lgnustep-base -lpthread -lob
jc -lm -I/usr/local/include/GNUstep -I/usr/include/GNUstep -fconstant-string-cla
ss=NSConstantString hello.m -o hello
In file included from /usr/include/GNUstep/Foundation/NSClassDescription.h:30:0,
from /usr/include/GNUstep/Foundation/Foundation.h:50,
from hello.m:1:
/usr/include/GNUstep/Foundation/NSException.h:42:2: error: #error The current se
tting for native-objc-exceptions does not match that of gnustep-base ... please
correct this.
i followed the http://ubuntuforums.org/showthread.php?p=5593608 as a reference.
I commented #error directive of NSException.h and problem is solved. Now i am getting new error.
/tmp/ccQlI9wJ.o: In function `main':
hello.m:(.text+0x11): undefined reference to `NSLog'
/tmp/ccQlI9wJ.o: In function `__objc_gnu_init':
hello.m:(.text+0x2a): undefined reference to `__objc_exec_class'
/tmp/ccQlI9wJ.o:(.data+0x40): undefined reference to `__objc_class_name_NSConsta
ntString'
collect2: ld returned 1 exit status
in Compile Objective-C Programs Using gcc
there is the following
Also note that if you did not include -D_NATIVE_OBJC_EXCEPTIONS, you
may run into the following error:
/usr/include/GNUstep/Foundation/NSException.h:42:2: error: #error The
current setting for native-objc-exceptions does not match that of
gnustep-base ... please correct this.
I has the same error as the original poster did, and passing the -D_NATIVE_OBJC_EXCEPTIONS flag fixed the problem for me.
I was trying to do something pretty non-standard, so it might not work for everyone.
Note that shalki's answer may also fix the issue. In case the link referenced there vanishes,
the blog post in question,
Compile Objective-C Programs on Linux
is in Chinese or Japanese or something like that, so I don't know exactly what it is saying, but I think the upshot is
to pass
`gnustep-config --objc-flags`
as an argument to gcc. The post has
gcc `gnustep-config --objc-flags` hello.m -o hello -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -lgnustep-base
at the end. Now, on my machine, gnustep-config --objc-flags expands to
-MMD -MP -DGNUSTEP -DGNUSTEP_BASE_LIBRARY=1 -DGNU_GUI_LIBRARY=1 -DGNU_RUNTIME=1 -DGNUSTEP_BASE_LIBRARY=1 -D_REENTRANT -fPIC -Wall -DGSWARN -DGSDIAGNOSE -Wno-import -g -O2 -fno-strict-aliasing -fexceptions -fobjc-exceptions -D_NATIVE_OBJC_EXCEPTIONS -fgnu-runtime -fconstant-string-class=NSConstantString -I. -I/home/faheem/GNUstep/Library/Headers -I/usr/local/include/GNUstep -I/usr/include/GNUstep
Yowza. Notice that this list of flags does contain-D_NATIVE_OBJC_EXCEPTIONS, along with lots of other stuff. For the record, my machine is running Debian squeeze. This might be a Debian/Ubuntu specific problem. I'm not sure.
Better write a GNUmakefile.
http://www.gnustep.it/nicola/Tutorials/WritingMakefiles/index.html
And better stop using gcc and switch to clang.

boost_python import error: module does not define init function

First off: I looked at the related questions, but they are not very helpful unfortunately. I'm trying to wrap an enum and a class from an external library.
#include <Python.h>
#include <boost/python.hpp>
using namespace boost::python;
#include <libvpsc/rectangle.h>
using vpsc::Rectangle;
using vpsc::Dim;
BOOST_PYTHON_MODULE(adaptagrams)
{
enum_<Dim>("dim")
.value("x", vpsc::XDIM)
.value("y", vpsc::YDIM)
.value("unset", vpsc::UNSET)
;
class_<Rectangle>("Rectangle",
init<double, double, double, double, optional<bool> >())
.add_property("centerX", &Rectangle::getCentreX)
.add_property("centerY", &Rectangle::getCentreY)
.add_property("width", &Rectangle::width, &Rectangle::set_width)
.add_property("height", &Rectangle::height, &Rectangle::set_height)
;
}
and compile with:
g++ -fPIC -I/usr/include/python2.7 -c adaptagrams.cpp -o adaptagrams.o
g++ -shared -Wl,-soname,adaptagrams.so -o adaptagrams.so adaptagrams.o -lpython2.7 -lboost_python -lvpsc
However, when I try to import the .so module, I get an error:
ImportError: dynamic module does not define init function (PyInit_adaptagrams)
Any ideas?
Update: When I restart Python and try the import, the first error I get is:
ImportError: ./adaptagrams.so: undefined symbol: _ZN8topology13computeStressERKSt6vectorIPNS_4EdgeESaIS2_EE
When I try it again, the 2nd one is the dynamic import from above (2.7) and a segfault (3.2). Boost is compiled against both 2.7 and 3.2 and I am linking the right ones on each approach.
Update 2: The tutorial code from the boost_python page works:
#include <Python.h>
#include <boost/python.hpp>
using namespace boost::python;
struct Hello
{
Hello(std::string msg): msg(msg) {}
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(constructor)
{
class_<Hello>("Hello", init<std::string>())
.def("greet", &Hello::greet)
.def("set", &Hello::set)
;
}
Same compilation:
g++ -fPIC -I/usr/include/python2.7 -c constructor.cpp -o constructor.o
g++ -shared -Wl,-soname,constructor.so -o constructor.so constructor.o -lpython2.7 -lboost_python
The name used in BOOST_PYTHON_MODULE must match the name of the .so library you generate and import into python.
I have seen this exception before. I got it using Visual Studio on windows, so things might be a little different over in unix-oid land but:
Two Possibilities:
Debug/Release miss-match:
You are trying to import a debug build of your module into a release build of python (or vice-versa). The solution is to include boost/python/detail/wrap_python.hpp instead of Python.h. This will fix some includes and defines to make it possible to do what you want.
Python/Boost.Python version miss-match:
Boost.Python is compiled against one specific version of python. You are using it with a different version. For example: you seem to be using python 2.7. Your boost_python library might be compiled against python 2.6. Yes, this means that your module can only work with one version of python at a time.
In addition to the other answers (in case another unfortunate soul runs unto this), make sure you're not accidentally compiling with the -fvisibility=hidden flag.
Doing so seems to strip the init function from the binary in both g++ / clang++.
Background info
In my case I had some trouble integrating some wrappers made with Boost.Python into a project. When built with the project's build-system I'd get the same runtime-error as OP (as opposed to building it with my proof-of-concept Makefile where it worked just fine).
Comparing the symbol tables with nm -g foo.so | grep Py showed me that in the non-working case the PyInit_* function was completely absent. Doing some comparison between compilation flags led me to -fvisibilty=hidden being the culprit.