Is possible to set some variable to Julia form another aplication ? (C++)

I run my Julia code nauka.jl by file.exe compiled in c++

{
system("C:\\Users\\PC\\AppData\\Local\\Julia-0.7.0\\bin\\julia.exe --history-file=no -L  nauka.jl");
}

Is possible to set into this sesion Julia some variable (string) ? At the moment or later via another exe file ?
Paul

I’m not entirely clear on what you’re trying to do but to answer the question I think you’re asking, yes it is possible to pass variables to julia from inside a C++ program. The proper way to do this is explained in the Embedding julia section of the manual. I suggest you have a read through that section of the manual and come back to ask another question here if you’re still having trouble.

I’ve already read this chapter but I’m not so advanced: /
1/ If in me code in file.exe is var x=5 .
2/ file.exe startig julia with code nauka.jl
3. I want send the x from my prog to Julia in this sesion.
file.exe before complilation C++:

#include<cstdlib>
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
int x = 5;
system("C:\\Users\\PC\\AppData\\Local\\Julia-0.7.0\\bin\\julia.exe --history-file=no -L  nauka.jl");

}

Paul

In general if you want to call C++ code from Julia it is much better to compile your code to a shared library and call it than to run a .exe separately.

2 Likes

Yes I see it !
But at tihis moment i need call from C++ app. to Julia ! Transfer var from c++ program to Julia.

Following up on @stevengj’s comment, it might be easier to help if we knew a bit more about you’re overall goal. Is your main code in C++? Would you like to run your C++ program, have it pass a value to julia, then have julia compute a result and pass it back to C++?

Or is your main program in julia and you need to call C++ to get some value needed by your julia code? Each of these scenarios requires a different approach. If you’re main program is in julia and you just need to get a value from C++ then you should take @stevengj’s approach, which is to compile your C++ code into a shared library and then use a tool like CxxWrap.jl to call the library fom Julia. Alternatively you could write a C interface for your library and use julia’s builtin ccall to call your C/C++ code.

If your main code is C++ then you need to use the embedding julia approach. You won’t be able to pass variables from C++ into julia if you’re just making a system call to run a julia script. You’ll have to access julia through its C API, as described in the Embedding Julia docs. I’m afraid there isn’t a simpler way to do it. I would suggest trying to make one of the simple examples in that section of the manual work and then try to adapt to your scenario. Start with the very first example in the section, in which you don’t even pass any arguments to julia. That example will at least let you check that you’re able to include the julia.h header file and link the julia libraries to your executable. Then move on to trying to pass arguments into functions in base julia. Then once you’re comfortable with all that you can move on to calling your own julia functions.

Thanks shared library this is good way to me, Very thanks!
Paul