How to use g++ or clang in my IDE?

I am creating a simple IDE that has an editor + compiler. It has syntax highlighting, code completion, etc. It returns the C++ code (it's a C++ IDE) in a string, I need to compile that string of C++ code and if there are errors let the compiler (g++ or clang) send the errors back to my program, so I could format the error output. Is that possible without adding the compiler's whole code base to my IDE source code? Or can I use something like fork() or exec() to do something of that kind? Please do do not be vague and try to link to external tutorials if possible. Try to be as straight-forward and as simple as possible.
Thanks, Abdullah.
Last edited on
k, add
#include <string>
to the libraries and try using

string variable="";

I think that will help..It will also be better if you send your information we correct it for you
> It returns the C++ code (it's a C++ IDE) in a string

The simplest way would be to write the string to a file

std::ofstream( temp_file_name ) << cpp_code_in_a_string << '\n' ;

Then invoke the compiler, redirecting the output to a file. For example:

1
2
const std::string compiler_cmd_line = "g++ -std=c++11 -Wall -Wextra -pedantic-errors " ;
std::string cmd = compiler_cmd_line + temp_file_name  + " 2> compiler_output.txt"


Then, open the file compiler_output.txt and parse for error messages. For instance, with g++,
main.cpp:17:5: error: <error diagnostic>

Indicates an error on line 17 col 5 of file main.cpp.
And how do I do that? How do I invoke the compiler?
I'm sure that IDEs like Code::Blocks and CodeLite don't do that. How can I make it part of the program?
OvO wrote:
I'm sure that IDEs like Code::Blocks and CodeLite don't do that.
Here is two commands invoked by C::B to compile project I use to quickly test code snippets:
-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------

[ 50.0%] x86_64-w64-mingw32-g++.exe -Wshadow -Winit-self -Wredundant-decls -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wswitch-enum -Wswitch-default -Wzero-as-null-pointer-constant -Weffc++ -Wmain -pedantic -std=c++11 -Wextra -Wall  -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wundef -Wfloat-equal -Winline -Wunreachable-code -Wswitch-enum -Wswitch-default -Wzero-as-null-pointer-constant -Wmain -pedantic -std=c++11 -Wextra -Wall -g -Wconversion -gdwarf-2   -IE:\Users\MiiNiPaa\home\Developement\lib\c++\BOOST\boost_1_54_0 -IE:\Users\MiiNiPaa\home\Developement\lib\c++\SFML  -c E:\Users\MiiNiPaa\home\Developement\CodeBlocks\projects\Test\main.cpp -o obj\Debug\main.o
[100.0%] x86_64-w64-mingw32-g++.exe  -o bin\Debug\Test.exe obj\Debug\main.o    -lgdi32 -luser32 -lkernel32 
As you can see, it is clearly calls g++ in command line
> And how do I do that? How do I invoke the compiler?

std::system(), (it is not 'evil'), typically to run a shell script (batch file).
http://en.cppreference.com/w/cpp/utility/program/system

Or if the toolchain is already in the path, directly. For instance:
1
2
3
const std::string compiler_cmd_line = "g++ -std=c++11 -Wall -Wextra -pedantic-errors " ;
std::string cmd = compiler_cmd_line + temp_file_name  + " 2> compiler_output.txt"
std::system( cmd.c_str() ) ;



I use a Linux, can it run bash scripts?
Last edited on
Last edited on
@giblit Thanks, just to say:
1
2
3
4
5
6
7
8
#include <iostream>
#define hey std::cout
#define say_good_bye return
int main() 
{
    hey << "C++ likes you! (Me too) ;) ";
    say_good_bye 0;
}


On retrospect, how do I pass the string containing the code to the script? Or even the filename that I saved the code in?
Last edited on
Topic archived. No new replies allowed.