The program get killed as soon as I inoke GetFamilyCount() so the "Exit" is not printed.
Invoking GetLastStatus() prints value 18 which is equals to GdiplusNotInitialized
I'm not sure why it's not initialized. You can clearly see that I initialized it here,
Gdiplus::PrivateFontCollection privateFontCollection;
I'm using MinGW to build the program.
MinGW-w64 9.0.0
GCC Version 11.1
OS is Windows 10
MinGW taken from http://winlibs.com/
#include <iostream>
#include <Windows.h>
#include <Gdiplus.h>
int main() {
std::cout << "Start" << "\n";
Gdiplus::PrivateFontCollection privateFontCollection;
std::cout << privateFontCollection.GetLastStatus() << "\n";
std::cout << privateFontCollection.GetFamilyCount() << "\n";
std::cout << "Exit" << "\n";
}
When debugging an app using std::cout to print something, nothing appears in the debug console (3rd tab, not external console). One of the exceptions seems to be the use of std::endl:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
while(true) {
Sleep(500);
std::cout << "Hello world!" << std::endl; // Works
}
}
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
while(true) {
Sleep(500);
std::cout << "Hello world!\n"; // Doesn't work
// std::cout << "Hello world!"; // Doesn't work
// std::cout << "Hello world!" << std::flush; // Doesn't work
}
}
In the first example our lines appear over time, while on the second doesn't appear at all (or the console refreshes somewhere around 60 secs). I think this is a bug but I'm not sure, so what is a workaround? Maybe I have to configure something instead? I found this problem while working on something else: C++: cannot see output in VS Code while debugging
Edit
The problem is what to do when u wanna output something without a new line
#include <iostream>
int main()
{
std::cout << "Hello World!" << std::endl;
return 0;
}
I was expecting to see only "Hello World" on the console after debugging. However, I see more stuff that distracts me, how can I fix that?
IMAGE:
http://prntscr.com/lt9jud
Code
#include <iostream>
#include <curses.h>
#include <stdlib.h>
int main() {
std::cout << "Hello, World!" << std::endl;
std::cout << "Hello, World!";
return 0;
}
ALL instruction/text I insert become grey... I just installed CLion, maybe I'm missing something?
CLion greys out includes that aren't used in your code.
std::cout is from iostream (which isn't greyed out)
Try Building Ctrl + F9 and debugging Shift + F9 your code, you'll see your outputs
Hello, World!
Hello, World!
Process finished with exit code 0
The following is my C++ program:
main.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
return 0;
}
When the above program is executed, it creates a text-file named "firstFile.cpp" containing the following code:
firstFile.cpp
#include <iostream>
int main() {
std::cout << "hello world" << std::endl;
return 0;
}
which, when executed, prints "hello world" on the screen.
So, I would like to add to the main.cpp file lines of code asking GCC to compile the new firstFile.cpp just created.
I am using GNU gcc on both platform Ubuntu and Windows.
Is it possible to get any error code form the call to the compiler? If not why.
This is not too difficult using the std::system command. Also raw string literals allow us to insert multiline text which is useful for typing in program parts:
#include <cstdlib>
#include <fstream>
// Use raw string literal for easy coding
auto prog = R"~(
#include <iostream>
int main()
{
std::cout << "Hello World!" << '\n';
}
)~"; // raw string literal stops here
int main()
{
// save program to disk
std::ofstream("prog.cpp") << prog;
std::system("g++ -o prog prog.cpp"); // compile
std::system("./prog"); // run
}
Output:
Hello World!
gcc is an executable, so you have to use either system("gcc myfile.cpp") or popen("gcc myfile.cpp"), which gives you a filestream as result.
But since you are generating code anyways, you don't even need to write it to a file. You can open the gcc proces with FILE* f = popen("gcc -x ++ <whatever flags>"). Then you have you can write your code with fwrite(f, "<c++ code>"). I know this is c and not really c++ but it might be useful. ( I don't think there is a c++ version of popen()).
All you need to do is add the following line after you create your file.
system("g++ firstFile.cpp -o hello");
Works on OS X so I hope it will work for you too.
To use the command line of a compiler in source file use system function.
Syntax of which is :
int system (const char* command); //built in function of g++ compiler.
In your case, it should be like
system("g++ firstFile.cpp");
PS: system function does not throw Exceptions.
Program
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
system("g++ firstFile.cpp");
return 0;
}
Depending on what you actually want to achieve you could also consider embedding some C++ compiler into your application.
Note that this is by far not as easy as calling an external executable, and might be subject to licence restrictions (GPL).
Also note that by using std::system or a similar mechanism you add the requirement on your target environment to actually have the called compiler available (unless you somehow bundle it with your application).
Something like this:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
system("c firstFile.cpp");
return 0;
}
or whatever command is appropriate for the compiler you're using.