Keith Sloan
Keith Sloan

Reputation: 55

Compliing C++ CGAL Module that is callable from Python

I am trying to compile some C++ code that is to be called from Python and then calls CGAL.

I need to be able to specify some flags on compilation, but not sure how to do this as the build process is

./CGAL-4.2/scripts/cgal_create_CMakeLists -s CallGCAL

cmake -DCGAL_DIR=./CGAL-4.2 .

make

And I don't want to have to hand edit the Makefile or CMakeLists

The error message I get from above without Flags to tell gcc it need Python are

CMakeFiles/CallGCAL.dir/CallGCAL.cpp.o: In function `initmyModule()':
CallGCAL.cpp:(.text+0x5e): undefined reference to `Py_InitModule4'
CMakeFiles/CallGCAL.dir/CallGCAL.cpp.o: In function `py_polygon(_object*, _object*)':
CallGCAL.cpp:(.text+0x86): undefined reference to `Py_BuildValue'
CMakeFiles/CallGCAL.dir/CallGCAL.cpp.o: In function `py_hull(_object*, _object*)':
CallGCAL.cpp:(.text+0xa6): undefined reference to `Py_BuildValue'
collect2: ld returned 1 exit status

Upvotes: 0

Views: 260

Answers (3)

lrineau
lrineau

Reputation: 6284

The CMakeLists.txt created by cgal_create_CMakeLists is not at all designed for your use case. The created CMakeLists.txt tries to compile an executable, whereas you want to create a module. In the CMakeLists.txt, replace the line:

create_single_source_cgal_program(CallCGAL.cpp)

by:

add_library(CallCGAL MODULE CallCGAL.cpp)
target_link_libraries(CallCGAL ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )

That way, CMake will create the correct Makefile, without any need to rename a file, or tweak the compilation and linking flags.

Upvotes: 0

Keith Sloan
Keith Sloan

Reputation: 55

Okay solved!!! Had to pass a variable to cmake.

Working script is as follows.

./CGAL-4.2/scripts/cgal_create_CMakeLists -s CallCGAL

cmake -DCGAL_DIR=./CGAL-4.2 -DCGAL_CXX_FLAGS="-shared -fPIC" .

make

rm -f CallCGAL.so

mv CallCGAL CallCGAL.so

Upvotes: 1

Walter
Walter

Reputation: 45444

Do I understand you correctly that want to compile your own code, which uses both Python and CGAL, with the aid of a makefile that comes with CGAL?

That is obviously impossible, since the CGAL makefile cannot know about Python. You must write your own makefile, which can be based on some existing makefiles, but also knows about Phython. The error you're reporting is generated by the loader which cannot find the Python library to resolve certain symbols. Thus, all you have to add to the makefile are the relevant -l and -L options.

Upvotes: 0

Related Questions