Building an example for win10
Hello! Previously, there were errors when building from source codes for windows 10 https://github.com/wlav/cppyy/issues/254. Now we've managed to build a fully working library from the source code for python 3.8.6. Before putting together large projects, I decided to try to put together a small example. Before that, I didn't really build projects using cmake on windows. After solving all the problems with the paths and receiving the whl package, there were errors. A little bit about the system on which everything is assembled:
- Windows 10
- cppyy 3.5.0 (and its dependencies, collected from source codes)
- llvm 21.1.1 (downloaded from here https://github.com/llvm/llvm-project/releases)
- python 3.8.6
- cmake version 3.31.6-msvc6
- Microsoft (R) C/C++ Optimizing Compiler version 19.44.35215 for x64 The assembly was carried out from powershell developer with access to MSVC.
exampleclass.h
#pragma once
#include <string>
#include <vector>
#include <memory>
#include <variant>
#include <cstdint>
class ExampleClass
{
public:
ExampleClass();
ExampleClass(int year, const std::string& name);
bool work();
void hello();
std::string hello(const std::string& name);
int square(int num);
void pow(double x, double y);
std::unique_ptr<std::vector<int>> vectorNumber();
struct Color {
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
uint8_t a = 255;
};
Color* color;
int m_var = 2;
private:
int m_priv_var = 0;
bool isPrivate();
};
exampleclass.cpp
#include "exampleclass.h"
#include <cmath>
#include <iostream>
#include <memory>
ExampleClass::ExampleClass()
{
}
ExampleClass::ExampleClass(int year, const std::string& name)
{
std::cout << "hello " + name + " you are " + std::to_string(year) + " years old" << std::endl;
}
bool ExampleClass::work()
{
return true;
}
void ExampleClass::hello()
{
std::cout << "hello" << std::endl;
}
std::string ExampleClass::hello(const std::string& name)
{
return "hello " + name;
}
int ExampleClass::square(int num)
{
return num * num;
}
void ExampleClass::pow(double x, double y)
{
std::cout << std::pow(x, y) << std::endl;
}
std::unique_ptr<std::vector<int>> ExampleClass::vectorNumber()
{
auto vect = std::make_unique<std::vector<int>>();
vect->push_back(2);
vect->push_back(12);
vect->push_back(687);
return vect;
}
bool ExampleClass::isPrivate()
{
return true;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(example-cppyy LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS ON)
set(llvm_path "C:/Program Files")
set(ROOT_CONFIG_DEBUG ON)
if(WIN32)
execute_process(
COMMAND python -c "import sys; print(sys.executable)"
OUTPUT_VARIABLE Python_EXECUTABLE
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
OUTPUT_VARIABLE PYTHON_SITE
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(LibClang_LIBRARY ${llvm_path}/LLVM/bin/libclang.dll)
set(Cppyy_DIR ${PYTHON_SITE}/cppyy_backend)
set(Cppyy_INCLUDE_DIRS ${PYTHON_SITE}/cppyy_backend/include)
execute_process(
COMMAND python -c "import sysconfig; print(sysconfig.get_path('scripts'))"
OUTPUT_VARIABLE PYTHON_SCRIPTS_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE)
set(Cppyygen_EXECUTABLE ${PYTHON_SCRIPTS_DIR}/cppyy-generator.exe)
set(Cppyy_EXECUTABLE ${PYTHON_SCRIPTS_DIR}/rootcling.exe)
endif()
message("python = ${Python_EXECUTABLE}")
message("python site-package = ${PYTHON_SITE}")
message("using clang = ${LibClang_LIBRARY}")
list(INSERT CMAKE_MODULE_PATH 0 ${PYTHON_SITE}/cppyy_backend/cmake)
set(CPPYY_MODULE_PATH ${PYTHON_SITE}/cppyy_backend/cmake)
if(WIN32)
set(ENV{LIBCLANG_LIBRARY} ${LibClang_LIBRARY})
set(ENV{PATH} "$ENV{PATH};${llvm_path}/LLVM/bin")
endif()
list(INSERT CMAKE_MODULE_PATH 0 ${PYTHON_SITE}/cppyy_backend/cmake)
set(CPPYY_MODULE_PATH ${PYTHON_SITE}/cppyy_backend/cmake)
set(Cppyy_DIR ${PYTHON_SITE})
set(Cppyy_INCLUDE_DIRS ${PYTHON_SITE}/cppyy_backend/include)
find_package(Cppyy REQUIRED)
set(ROOT_DIR ${PYTHON_SITE}/cppyy_backend/cmake)
set(ROOT_INCLUDE_DIRS ${PYTHON_SITE}/cppyy_backend/include)
find_package(ROOT 6.16 REQUIRED)
include("${ROOT_USE_FILE}")
message("ROOT_FOUND: ${ROOT_FOUND}")
set(ROOT_LIBRARIES ${ROOT_LIBRARY_DIR}/libRIOLegacy.lib)
message("ROOT_LIBRARIES: ${ROOT_LIBRARIES}")
message("ROOT_LIBRARY_DIR: ${ROOT_LIBRARY_DIR}")
set(HEADERS exampleclass.h)
set(SOURCES exampleclass.cpp)
set(CMAKE_INCLUDE_DIRECTORIES_BEFORE ON)
add_library(examplePy SHARED ${HEADERS} ${SOURCES})
set_target_properties(examplePy PROPERTIES
WINDOWS_EXPORT_ALL_SYMBOLS ON
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
POSITION_INDEPENDENT_CODE ON
)
find_library(CPPYY_BACKEND_LIB NAMES cppyy_backend PATHS ${PYTHON_SITE}/cppyy_backend/lib)
list(APPEND CPPYY_BACKEND_LIB C:/Users/itain/AppData/Local/Programs/Python/Python38/Lib/site-packages/cppyy_backend/lib/libCoreLegacy.lib)
message("CPPYY_BACKEND_LIB: ${CPPYY_BACKEND_LIB}")
target_include_directories(examplePy PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(examplePy PUBLIC ${CPPYY_BACKEND_LIB})
if(WIN32)
target_include_directories(examplePy PUBLIC ${llvm_path}/LLVM/include)
target_link_directories(examplePy PUBLIC ${llvm_path}/LLVM/lib)
target_compile_definitions(examplePy PUBLIC
-D_UNICODE
-DUNICODE
-D_CRT_SECURE_NO_WARNINGS
-D_SCL_SECURE_NO_WARNINGS
)
target_compile_options(examplePy PRIVATE
-Wall
-fPIC
/utf-8
/EHsc
)
endif()
#add_executable(${PROJECT_NAME} "main.cpp")
#target_link_libraries(${PROJECT_NAME} Qt5::Core examplePy)
link_directories(${CMAKE_BINARY_DIR})
ROOT_GENERATE_DICTIONARY(
examplePyWrap
${HEADERS}
MODULE examplePy
)
cppyy_add_bindings(
"examplePyWrap"
"0.1.0"
"Ivan"
"Ivan email"
LANGUAGE_STANDARD "14"
GENERATE_OPTIONS ""
INCLUDE_DIRS
${Qt5Core_INCLUDE_DIRS}
${Qt5Gui_INCLUDE_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}
${PYTHON_SITE}/cppyy_backend/include
LINK_LIBRARIES ${CMAKE_BINARY_DIR}/examplePy.lib ${CPPYY_BACKEND_LIB}
H_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_BINARY_DIR}
H_FILES ${HEADERS}
)
add_dependencies(examplePyWrapCppyy examplePy)
Error 1
>>> from examplePyWrap import *
input_line_21:1:1: error: source file is not valid UTF-8
<C0><F3><F7> <A6><U+0001>
^
input_line_21:1:2: error: source file is not valid UTF-8
<C0><F3><F7> <A6><U+0001>
^
input_line_21:1:3: error: source file is not valid UTF-8
<C0><F3><F7> <A6><U+0001>
^
input_line_21:1:5: error: source file is not valid UTF-8
<C0><F3><F7> <A6><U+0001>
^
input_line_21:1:6: error: expected expression
<C0><F3><F7> <A6><U+0001>
^
cling::DynamicLibraryManager::loadLibrary() [C:\example-cppyy\build\examplePyWrap\examplePyWrapCppyy.dll -> C:\example-cppyy\build\examplePyWrap\examplePyWrapCppyy.dll]: LoadLibrary: returned 1114: ╧Ёюшчю°хы ёсющ т яЁюуЁрььх шэшЎшрышчрЎшш сшсышюЄхъш фшэрьшўхёъющ ъюьяюэютъш (DLL).
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\example-cppyy\build\examplePyWrap\__init__.py", line 4, in <module>
initialise('examplePyWrap', 'examplePyWrapCppyy.dll', 'examplePyWrap.map')
File "C:\example-cppyy\build\examplePyWrap\initializor.py", line 192, in initialise
cppyy.load_reflection_info(os.path.join(pkg_dir, lib_file))
File "C:\Users\itain\AppData\Local\Programs\Python\Python38\lib\site-packages\cppyy\_cpython_cppyy.py", line 198, in load_reflection_info
raise RuntimeError("Unable to load reflection library "+name)
RuntimeError: Unable to load reflection library C:\example-cppyy\build\examplePyWrap\examplePyWrapCppyy.dll
Error 2
>>> import cppyy
>>> cppyy.include("C:/example-cppyy/exampleclass.h")
True
>>> cppyy.load_library("C:/example-cppyy/build/Debug/examplePy.dll")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\itain\AppData\Local\Programs\Python\Python38\lib\site-packages\cppyy\__init__.py", line 270, in load_library
sc = gSystem.Load(name)
File "C:\Users\itain\AppData\Local\Programs\Python\Python38\lib\site-packages\cppyy\__init__.py", line 202, in __exit__
self.err = _end_capture_stderr()
File "C:\Users\itain\AppData\Local\Programs\Python\Python38\lib\site-packages\cppyy\_cpython_cppyy.py", line 204, in _end_capture_stderr
err = _backend._end_capture_stderr()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 337: invalid continuation byte
After the build, I installed the resulting whl package and tried to download it and got Error1. When trying to download the h and dll file directly, I get Error2. At the moment, I do not know how to solve this problem. All attempts to solve it were unsuccessful. Have you ever encountered anything like this, or do you have any thoughts on how to resolve it? Thank you in advance!