0

Working on a C++ based application, it takes user input and generates a C++ function and compile it to create a .so file and links the function to the main application. Currently had to call an external command "g++" to do it. Wonder if it's possible to call some kind of function, say, "compile" which takes as input an code snippet and produces an .so. More precisely, I need a function that has the following syntax:

sizeOfObjBuf = compile(codeBuf, objBuf); 

First parameter is a null terminated string containing a code snippet, the second parameter is the output buffer that hold the compiled code and it returns the size of size of compiled code.

The whole idea is to get rid of dependency on an external program (g++) so the application can run on any Linux system (even when it doesn't have g++ installed).

Thanks.

2
  • You're essentially looking for a C++ compiler-as-a-library, which I have not seen. Given C/C++'s reliance on a decent number of header and related library files I'm not sure it's practical to wrap it all up inside a library. Commented Apr 29, 2014 at 1:14
  • 1
    You aren't going to save much by not having g++ installed. Any compiler is going to require megabytes of header files and library support. Commented Apr 29, 2014 at 1:18

3 Answers 3

1

I'm afraid the answer is "no".

Sign up to request clarification or add additional context in comments.

2 Comments

I guess g++ itself probably will depend on some sort of compiling libraries to do its job. If I can call that library, that would be super.
No, GCC is not a library. It is a (set of) programs. Indeed g++ is just a driver program, the real compilation is done by /usr/lib/gcc/x86_64-linux-gnu/4.8/cc1plus
1

You could implement that function by executing G++ (or some other compiler) in a separate process and waiting for it to finish, but that still requires the user to have a compiler installed.

You can't compile C++ code without a C++ compiler.

3 Comments

I would to use the same compiling library that C++ compiler (g++) uses. Thanks.
G++ doesn't use a "compiling library"; the g++ command-line program directly invokes the various build stages (e.g. compiler, assembler, linker). The Clang compiler, however, does have a library interface, so that might be what you're looking for. But users of your program will still need to install the Clang library.
thanks for mentioning CLang too. I will check it out.
1

I am not going to do the research to figure out how it is done, but I believe the LLVM C++ compiler can be used in this way. All of the parts of LLVM are designed to run as a library, in theory.

OK, a tiny bit of research and I found this: http://clang.llvm.org/docs/LibTooling.html

1 Comment

Thanks for the link to LLVM and Clang, will check it out.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.