Hi Joost,
you're missing the newlines in the text source, at least after the
#include line. This works for me:
"#include <iostream> \n"
"void test() \n"
"{\n"
"std::cout << \"test()\" << std::endl;\n"
"}\n";
Cheers, Axel.
Joost Kraaijeveld wrote on 10/31/2009 08:23 AM:
> Hi,
>
> I am trying to run a null terminated string a source code (for the full
> source of my program see below). It fails however with the following
> output:
>
> + callMemoryString()
> tempFileName: (tmp0)
> result G__exec_text( test()): 0
> Error: Function test() is not defined in current scope + callMemoryString()
> FILE:(tmpfile) LINE:2
> !!!Dictionary position rewound... !!!Error recovered!!!
>
> Using this as actual source code works:
>
> std::string code =
> "void test() "
> "{"
> "printf(\"Kees Kroket\\n\");"
> "}
>
> What is the correct way of doing this?
>
> TIA
>
>
> /////////////////////////////// The code
>
> #include <iostream>
> #include "G__ci.h"
> #include <string>
>
> void callMemoryString()
> {
> std::cout << "+ callMemoryString()" << std::endl;
>
> int state = G__init_cint( "cint");
> switch (state)
> {
> case G__INIT_CINT_SUCCESS:
> {
> std::string code =
> "#include <iostream> "
> "void test() "
> "{"
> "std::cout << \"test()\" << std::endl;"
> "}";
>
> const char* tempFileName = G__load_text(code.c_str());
> if(tempFileName)
> {
> std::cout << "tempFileName: " << tempFileName << std::endl;
>
> long result = 0;
> G__value resultValue;
>
> resultValue = G__exec_text( "test()");
> result= G__int(resultValue);
> std::cout << "result G__exec_text( test()): " << result << std::endl;
>
> G__unloadfile(tempFileName);
> }else
> {
> std::cout << "Could not create a temp file" << std::endl;
> }
>
> break;
> }
> case G__INIT_CINT_SUCCESS_MAIN:
> {
> std::cout << "G__INIT_CINT_SUCCESS_MAIN" << std::endl;
> break;
> }
> case G__INIT_CINT_FAILURE:
> {
> std::cout << "G__INIT_CINT_FAILURE" << std::endl;
> break;
> }
> default:
> {
> std::cout << "G__init_cint default" << std::endl;
> }
> }
> G__scratch_all();
>
> std::cout << "+ callMemoryString()" << std::endl;
> }
> int main(int argc, char** argv)
> {
> callMemoryString();
> return 0;
> }
>