c++ sources compilation at runtime

D

davide galassi

Hi there,
it's possible under windows to compile a c++ source at runtime? Exists
a simple open source library to include in my project that include a
function such as COMPILE( "FilePath.cpp" ) and output for me a dll....

I want to compile (as a dll) a source file that references an external
library from the main program (written in C++)

Thanks a lot
David
 
Ö

Öö Tiib

Hi there,
it's possible under windows to compile a c++ source at runtime?

Yes. Most IDE-s are running when they invoke other programs (called
compilers) to compile c++ source.
Exists
a simple open source library to include in my project that include a
function such as COMPILE( "FilePath.cpp" ) and output for me a dll...

No. But why you need it? It is simple already if it is Windows SDK
that you are using. There is CreateProcess() that you can supply with
full command line to invoke your compiler. You can
WaitForSingleObject() until it finishes and then LoadLibrary() or
LoadLibraryEx() that freshly made dll if all went well. Read docs for
details.
I want to compile (as a dll) a source file that references an external
library from the main program (written in C++)

What are you doing? Viruses that self-compile?
 
D

davide galassi

Yes. Most IDE-s are running when they invoke other programs (called
compilers) to compile c++ source.

Emmm... i know what a compiler is since i've used c++,c,and x86
assembly in the last 8/9 years... the problem that i asked for is very
specific. And i simply asked if exist a runtime cross platform
compiler library...

No. But why you need it? It is simple already if it is Windows SDK
that you are using. There is CreateProcess() that you can supply with
full command line to invoke your compiler. You can
WaitForSingleObject() until it finishes and then LoadLibrary() or
LoadLibraryEx() that freshly made dll if all went well. Read docs for
details.


What are you doing? Viruses that self-compile?

Eheheheheh... NO actually i'm not writing a virus... i'm writing my
thesis and it's about a software rasterizer. The user of my engine
simply have to write the pixel and the vertex shaders programs in c++,
passing the path to the source to the engine that compile the
microprograms as dll and jumps to the code when it's needed. So No
virus stuff just plugins.
Why when someone ask for somethin unusual and triky the people always
thinks that it's a newby trying to write some stupid virus? :)

So exist a way to compile c++ code at runtime?
I've looked at the jitlib but i just need something that do this job

file.cpp -> dll and then my engine loads the dll and simpy use it.

I start thinking that i'll have to invoke the gcc compiler at
runtime...but this solution doesn't look very cool...

Thaks again
Dave
 
G

gwowen

I start thinking that i'll have to invoke the gcc compiler at
runtime...but this solution doesn't look very cool...

You want to compile some c++ code, but you don't want to run a c++
compiler because that's "not cool"?!? Seriously? That's your
complaint -- calling a compile() function is "cooler" than calling
CreateProcess("your_compiler_here")?

The mind just boggles.
 
V

Victor Bazarov

[..] i'm writing my
thesis and it's about a software rasterizer. The user of my engine
simply have to write the pixel and the vertex shaders programs in c++,

I believe shaders are *usually* written in the graphics card language,
no? It's not C++, but it looks close. What I'm trying to say is that
asking users to be versed in C++ may be too much.
passing the path to the source to the engine that compile the
microprograms as dll and jumps to the code when it's needed. So No
virus stuff just plugins.
Why when someone ask for somethin unusual and triky the people always
thinks that it's a newby trying to write some stupid virus? :)

So exist a way to compile c++ code at runtime?

Store your function in a file (temporary). Invoke a compiler with a
command line and give it that file as the source. You control the
switches, you control the name of the source, you control the result.
I've looked at the jitlib but i just need something that do this job

file.cpp -> dll and then my engine loads the dll and simpy use it.

I start thinking that i'll have to invoke the gcc compiler at
runtime...but this solution doesn't look very cool...

Neither does using C++ at run-time. Ask in 'comp.graphics.*' hierarchy,
they might know more about how to efficiently write shaders to be used
at run-time.

V
 
J

Jorgen Grahn

What are you doing? Viruses that self-compile?

Actually, that's a technique I think is underused today, when
compilers are free and fit almost anywhere.

Take firewalling/packet filtering rules for example -- you can either
have a general-purpose function which takes a config file, or you can
generate (with the help of a C++ compiler) optimal code from such a
config file.

/Jorgen
 
D

davide galassi

[..] i'm writing my
thesis and it's about a software rasterizer. The user of my engine
simply have to write the pixel and the vertex shaders programs in c++,

I believe shaders are *usually* written in the graphics card language,
no?  It's not C++, but it looks close.  What I'm trying to say is that
asking users to be versed in C++ may be too much.
passing the path to the source to the engine that compile the
microprograms as dll and jumps to the code when it's needed. So No
virus stuff just plugins.
Why when someone ask for somethin unusual and triky the people always
thinks that it's a newby trying to write some stupid virus? :)
So exist a way to compile c++ code at runtime?

Store your function in a file (temporary).  Invoke a compiler with a
command line and give it that file as the source.  You control the
switches, you control the name of the source, you control the result.
I've looked at the jitlib but i just need something that do this job
file.cpp ->  dll and then my engine loads the dll and simpy use it.
I start thinking that i'll have to invoke the gcc compiler at
runtime...but this solution doesn't look very cool...

Neither does using C++ at run-time.  Ask in 'comp.graphics.*' hierarchy,
they might know more about how to efficiently write shaders to be used
at run-time.

V


OK! maybe i've not described very well what is my problem :)

I know that the shaders for dx are written in HLSL language. I know
that shaders for opengl are written in cg shader language.
I'm writing an engine that runs completly in software... and simulate
a real device.
My engine uses c++ for the shaders.

Since now i've compiled offline my shaders as dlls and the engine
loads the shaders and simply jumps to the code when it's needed to
process the vertices... but now i want to give the possibility to the
user to don't know how to compile the shaders as dlls but simply write
a c++ program and pass it to the engine as a source code.

The engine now compiles the c++ programs and use them..

I posted here the question because the problem is not what i have to
do... the problem is simply : EXIST an open source library to compile
a c++ source at runtime?

i know that to compile a source i have to invoke a compiler... but i
don't want to include gcc or the visual stdio compiler as a binary in
my engine. instead i want to include the sources of a library to
compile at runtime.

I found the TCC compiler (http://bellard.org/tcc/) that has the LIBTCC
to compile at runtime a C program passed as a string in memory. but i
need a c++ solution.

PS: i want to do this because a want that the user of my engine don't
have to know how internally the engine will execute his c++ source
code and he simply have to write a c++ program and pass the path to
the engine...

I think now i've been more clear.

Thanks
David
 
V

Victor Bazarov

[..] EXIST an open source library to compile
a c++ source at runtime?

I don't know of any. However, knowing that GCC is open source, making
it into a library is probably not such a huge undertaking...
i know that to compile a source i have to invoke a compiler... but i
don't want to include gcc or the visual stdio compiler as a binary in
my engine. instead i want to include the sources of a library to
compile at runtime.

I found the TCC compiler (http://bellard.org/tcc/) that has the LIBTCC
to compile at runtime a C program passed as a string in memory. but i
need a c++ solution.

PS: i want to do this because a want that the user of my engine don't
have to know how internally the engine will execute his c++ source
code and he simply have to write a c++ program and pass the path to
the engine...

I think now i've been more clear.

Yes, so shaders have nothing to do with it. I get it.

Allowing user to execute any arbitrary C++ code inside your process as a
DLL is *extremely* dangerous and open to security issues nobody wants to
deal with, and that's probably why this has never been considered of any
interest, and that's why there is no solution (even partial) to do what
you think you need. Consider a C++ interpreter. Google for it. The
performance is not there, but at least it will do *some* of what you
looking for.

If the language is not the limitation (and you're open to trying other
solutions like Python or Ruby), there are embedded interpreters on the
market, and some are probably free.

V
 
Ö

Öö Tiib

OK! maybe i've not described very well what is my problem :)

Yes, sorry for guessing of viruses wildly.
I know that the shaders for dx are written in HLSL language. I know
that shaders for opengl are written in cg shader language.
I'm writing an engine that runs completly in software... and simulate
a real device.
My engine uses c++ for the shaders.

Okay. If it was my thesis i would change it like "My engine uses
Python for the custom shaders." Integrating Python interpreter is
really easy and that language provides lot less complications since it
is meant for scripting. If you hate syntax of Python then you may use
interpreter with more C-like syntax like squirrel and others.
Since now i've compiled offline my shaders as dlls and the engine
loads the shaders and simply jumps to the code when it's needed to
process the vertices... but now i want to give the possibility to the
user to don't know how to compile the shaders as dlls but simply write
a c++ program and pass it to the engine as a source code.

Yes. But there are no integrated C++ compilers. C++ compilers are
large and complex beasts. You do not have to provide one with your
program. gcc is freely downloadable and installable. Your program can
check if it is installed and if it is not then refuse to let users to
provide their shaders in C++. Otherwise your program invokes installed
gcc.
The engine now compiles the c++ programs and use them..

I posted here the question because the problem is not what i have to
do... the problem is simply : EXIST an open source library  to compile
a c++ source at runtime?
No.

i know that to compile a source i have to invoke a compiler... but i
don't want to include gcc or the visual stdio compiler as a binary in
my engine. instead i want to include the sources of a library to
compile at runtime.

No need. They can be kept separate things with soft dependency. No
compiler installed, no custom shaders compiled run time.
I found the TCC compiler (http://bellard.org/tcc/) that has the LIBTCC
to compile at runtime a C program passed as a string in memory. but i
need a c++ solution.

PS: i want to do this because a want that the user of my engine don't
have to know how internally the engine will execute his c++ source
code and he simply have to write a c++ program and pass the path to
the engine...

I think now i've been more clear.

Crystal clear. I do not know anything that matches exactly with your
needs. Compiling C++ is rare ability so there are only handful of
useful C++ compilers on our planet. All have little bugs and oddities
in them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top