1

I need to compile and link some code at runtime. I am using the approach suggested here: https://stackoverflow.com/a/10565120/3038460

Basically I am writing my code into a .cpp file and then compiling with

/usr/bin/g++ -shared mysource.cpp -o libname.so

Everything works fine while I only #include headers from standard libraries. But what if I need to use a custom class within the 'dynamic' code? How can include my own header? The .cpp file is temporary stored in the same location of my binary file and this might be different than the location of my source files. Is there a way to know at runtime where the original source code was located? I doubt.

Moreover I would like my code to work even if the original source code is not available.

To clarify, mysource.cpp might look like this:

#include <stdlib.h>
#include <vector>
#include "myheader.h"     <---- how can g++ find this file? At runtime,
                                when I create mysource.cpp, I have no idea
                                where myheader.h is located

void f(){
  // Code
}

What's the best solution to solve this problem?

Community
  • 1
  • 1
Marco Ancona
  • 2,073
  • 3
  • 22
  • 37
  • Your goal of not having the rest of the source code available isn't really compatible with C/C++ development. You typically need to include headers because you need access to data type definitions and/or function prototypes that define the interfaces that you want to utilize. If those headers aren't available, then the compiler doesn't know how to compile the code snippet that you give it. C/C++ aren't designed to be able to incrementally compile when only binaries are available. – Jason R Apr 14 '15 at 00:26
  • Ok... so I guess the only way is to print the content of the header directly inside the `mysource.cpp`, taking it from a hard-coded string or copying the source file with a script as suggest by @Z̷͙̗̻͖̣̹͉̫̬̪̖̤͆ͤ̓ͫͭ̀̐͜͞ͅͅαлγo – Marco Ancona Apr 14 '15 at 00:37

1 Answers1

0

Generate the contents of that header file into the generated source file.

Technically, an #include directive is just a directive to include the full contents of a header file in the body of a source file (specifically, at the point where you included it) at compile time. You can thus skip the middleman and generate the contents of that header file into your source file before compiling it instead of an #include

  • Why not add the contents of the header file to the top of the generated source file then (possibly below other includes)? That's basically all a header file does anyway, adding its code at the point it was generated. – The name's Bob. MS Bob. Apr 14 '15 at 00:17
  • I considered this solution, but this implies that I should hardcode the header as a string and modify the string every time I change the source file to keep it consistent. It's not a big deal, but I was trying to figure out if there is a better solution because this is not elegant at all in my opinion – Marco Ancona Apr 14 '15 at 00:25
  • Find a way to automatically generate that string on the backend then. – The name's Bob. MS Bob. Apr 14 '15 at 00:26
  • It would be great is the program itself could generate a string with the content of its own source code. But I don't think this is possible. The only solution I can think of is to run a script every time the program is compiled to copy the source file in the location of the binary. Then reading from there is easy. – Marco Ancona Apr 14 '15 at 00:28
  • 1
    I'm not suggesting making the program a quine. :) But yeah, I'd add a script that gets run before compilation in the makefile. – The name's Bob. MS Bob. Apr 14 '15 at 00:29