Let's try to tackle the plethora of issues that have cropped up during the lengthy discussion, one by one.
Question 1: Why do I get a segfault when using some non-standard parameters (like string vector or int pointer) to main
?
The parameter types of int, char **
are defined that way by both the C and the C++ standard. Non-standard extensions aside, you cannot use other types.
From ISO/IEC 9899 (The C Language), 5.1.2.2.1 Program startup:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int
and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc
and argv
, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent; or in some other implementation-defined manner.
That last sentence allows for those extensions I mentioned. One such extension I know of is GCC's environ
:
https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html#Program-Arguments
Question 2: How do I hack around this?
You don't.
Using different types than those defined by the standard, or by compiler extensions, is Undefined Behavior, which can -- but does not need to -- lead to segfaults. Do not invoke undefined behavior. Do not "hack around" the standard. It is not a "workaround", let alone a "solution", it is broken code that can blow up in your face any time.
Question 3: How do I pybind
a third-party function that takes a char **
as parameter?
You don't, as this is not a datatype supported by pybind.
Question 4: How do I interface such a function through pybind
, then?
You write a wrapper function that, on the front end, takes parameters supported by pybind
(e.g. std::vector< std::string >
), appropriately marshals those, and then calls the third-party backend function for you with the marshalled arguments. (Then, of course, doing the same in reverse for the return type, if required.)
For an idiomatic example on how to do that, see the answer by @TedLyngmo.
Question 5: Can I pybind
to a third-party main
?
This is ill-advised, as main
is a special function, and the called code may make assumptions (like atexit
callbacks) that your calling code does not, and can not, comply with. It is certainly not a function the third party ever expected to be called as a library function.