0

I'm trying to build a *.whl python package from C++ source using pybind11 and dlib.

This GCC works great for me:

gcc -O3 -Wall -shared -I /home/user/dlib /home/user/dlib/dlib/all/source.cpp -lpthread -lX11 -ljpeg -lpng -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT -std=c++11 -fPIC `python3 -m pybind11 --includes` age_and_gender.cpp -o age_and_gender`python3-config --extension-suffix`

Now i'm trying to build *.whl python package from this example: https://github.com/pybind/cmake_example

My CMakeLists.txt file:

cmake_minimum_required(VERSION 2.8.12)

if (CMAKE_VERSION VERSION_LESS 3.0)
  PROJECT(example_app CXX)
else()
  cmake_policy(SET CMP0048 NEW)
  PROJECT(example_app VERSION "1.0.0" LANGUAGES CXX)
endif()

add_compile_options(
    -O3
    -Wall
    -shared
    -fPIC
    -lpthread
    -lX11
    -ljpeg
    -lpng
    -DDLIB_JPEG_SUPPORT
    -DDLIB_PNG_SUPPORT
    -std=c++11
)

add_executable(dlib_source "/home/user/dlib/dlib/all/source.cpp")
include_directories("/home/user/dlib")

add_subdirectory(pybind11)
pybind11_add_module(example_app "src/main.cpp")

I think this is not the correct version of CMakeLists.txt. Help compose the correct CMakeLists.txt file given my "gcc" command that works.

When I try to import the *.whl package, I got this error:

from example_app import *
Segmentation fault (core dumped)
Mowshon
  • 939
  • 9
  • 16
  • 2
    "I think this is not the correct version" - You can check that by using `make VERBOSE=1` when building your CMake project and comparing command line, emitted by CMake, with one which "works great for me". Being pedantic I see that `age_and_gender.cpp` is missed in your CMake project. Being less pedantic: `-l` flag is [not a compiler option](https://stackoverflow.com/questions/43136418/how-to-add-l-ell-compiler-flag-in-cmake), CMake has its own mechanisms for `-fPIC`, `-std=c++11`, `-lpthread` which could be better then direct ones. – Tsyvarev Jun 15 '20 at 19:17
  • @Tsyvarev how can write this `-I /home/user/dlib /home/user/dlib/dlib/all/source.cpp` in cmake file? Thank you for answer. – Mowshon Jun 16 '20 at 15:05
  • 1
    These are actually two **independent** parameters. The first one, `-I /home/user/dlib` is a specification of **include directory**, in CMake you may use `include_directories` command for that purpose. The second one, `/home/user/dlib/dlib/all/source.cpp`, is a **source file** specification. You have already specified it in `add_executable` command. – Tsyvarev Jun 16 '20 at 15:17
  • @Tsyvarev the last question :D pls. How cam I write this `-DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT` in cmake ? – Mowshon Jun 16 '20 at 17:00
  • 1
    Command [add_compile_options](https://cmake.org/cmake/help/v3.9/command/add_compile_options.html) is a valid way for specify macro definitions, but [add_definitions](https://cmake.org/cmake/help/v3.9/command/add_definitions.html) is a better choice. – Tsyvarev Jun 16 '20 at 17:15

0 Answers0