My project structure looks like this:
emb
| CMakeLists.txt
| main.c
| python35.lib
| stdlib.zip
| _tkinter.pyd
|
+---include
| |
| | abstract.h
| | accu.h
| | asdl.h
...
| | warnings.h
| | weakrefobject.h
|
+---build
| | emb.exe
stdlib.zip contains the DLLs, Lib and site-packages directories from Python 3.5.2 installation whose paths are appended to sys.path
. I'm implicitly loading python35.dll by linking to python35.lib which contains the stubs for all of the exported functions in the DLL. Here's the contents of CMakeLists.txt:
cmake_minimum_required(VERSION 3.6)
project(embpython)
set(SOURCE_FILES main.c)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
set(PYTHON_INCLUDE_DIR include)
include_directories(${PYTHON_INCLUDE_DIR})
target_link_libraries(
${PROJECT_NAME}
${CMAKE_CURRENT_LIST_DIR}/python35.lib
${CMAKE_CURRENT_LIST_DIR}/_tkinter.pyd)
And here's the contents of main.c:
#include <Python.h>
int main(int argc, char** argv)
{
wchar_t* program_name;
wchar_t* sys_path;
char* path;
program_name = Py_DecodeLocale(argv[0], NULL);
if (program_name == NULL)
{
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program_name);
path = "stdlib.zip;stdlib.zip/DLLs;stdlib.zip/Lib;"
"stdlib.zip/site-packages";
sys_path = Py_DecodeLocale(path, NULL);
Py_SetPath(sys_path);
Py_Initialize();
PySys_SetArgv(argc, argv);
PyRun_SimpleString("import tkinter\n");
Py_Finalize();
PyMem_RawFree(sys_path);
PyMem_RawFree(program_name);
return 0;
}
Now, here's the error I'm getting:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File " ... emb\stdlib.zip\Lib\tkinter\__init__.py", line 35, in <module>
ImportError: DLL load failed: The specified module could not be found.
What am I doing wrong and how can I fix it?