I am trying to embedded a python script within a C++ application. I am trying to do it without having the need to use the argv parameters and therefore I am trying to follow this tutorial: https://www.codeproject.com/Articles/820116/%2FArticles%2F820116%2FEmbedding-Python-program-in-a-C-Cplusplus-code
My first step was to run this tutorial (1.1 Very High Level embedding): https://docs.python.org/3/extending/embedding.html which is now working.
While going to the second tutorial, my code compiled but the script doesn't seems to be loaded as nothing is displayed in the console
main.cpp:
#include <iostream>
#include <string>
#include <Python.h>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
char filename[] = "test_script.py";
FILE* fp;
Py_Initialize();
fp = _Py_fopen(filename, "r");
PyRun_SimpleFile(fp, filename);
Py_Finalize();
return 0;
}
test_script.py:
print("print test from python")
def multiply(a,b):
print("Will compute", a, "times", b)
c = 0
for i in range(0, a):
c = c + b
return c
CmakeList.txt
cmake_minimum_required(VERSION 3.13)
project(TestCharacter)
set(CMAKE_CXX_STANDARD 14)
find_package(PythonLibs REQUIRED)
add_executable(TestCharacter main.cpp)
target_include_directories(TestCharacter PRIVATE ${PYTHON_INCLUDE_DIRS})
target_link_libraries(TestCharacter PRIVATE ${PYTHON_LIBRARIES})
As I don't get any building error I don't know where to look for. I have tried to modify the script's name within my C++ code as to force an error (I first thought that it may be a path or name error), but I didn't get any building error