scdmb
scdmb

Reputation: 15621

How to check programmatically that source compiles?

There is some string with code:

std::string code = "int main(){return 0;}"

What tool can I use to check whether this is valid C++ code? Something similar to

checker.checkCPPcode(code) which returns bool

would be nice.

Upvotes: 3

Views: 166

Answers (2)

Luchian Grigore
Luchian Grigore

Reputation: 258618

You can paste the content of the string in a newly created cpp file and run g++ or whatever compiler you can use on it and check the compiler's output.

Upvotes: 2

thiton
thiton

Reputation: 36049

A C++ compiler like g++ will do the job. Write the string into a temporary file, run g++ -fsyntax-only on the file, and act on the return value.

Upvotes: 6

Related Questions