It doesn't look like it needs to be main
after all, so you could do like this:
#include
#include
#include
int cppmain(std::string program, std::vector args) {
std::cout << program << " got arguments:\n";
for(auto& arg : args) {
std::cout << " " << arg << "\n";
}
return 0;
}
int main(int argc, char* argv[]) {
// create a string from the program name and a vector of strings from the arguments
return cppmain(argv[0], {argv + 1, argv + argc});
}
In case you need to call a closed source main-like function (that you can not change), create a wrapper function that you can pybind
to and let that function call the closed source function.
#include
#include
#include
#include
int closed_source_function(int argc, char* argv[]) {
for(int i = 0; i < argc; ++i) {
std::cout << argv[i] << '\n';
}
return 0;
}
int pybind_to_this(std::vector args) {
// create a char*[]
std::vector argv(args.size() + 1);
// make the pointers point to the C strings in the std::strings in the
// std::vector
for(size_t i = 0; i < args.size(); ++i) {
argv[i] = args[i].data();
}
// add a terminating nullptr (main wants that, so perhaps the closed source
// function wants it too)
argv[args.size()] = nullptr;
// call the closed source function
return closed_source_function(static_cast(args.size()), argv.data());
}