ok its actually easier then you think.
Code:
#include <cstdio>
int main()
{
printf("hello, my name is Charlie ");
printf("When i\'m done i\'m going to put serious C programmer on my curriculum vitae ");
printf( " \n ");
return 0;
}
open a linux text editor i use geany copy and paste and then save the above as "charlie.cpp"
say on your Desktop. Then open a terminal and:
cd Desktop
note there should be no need for sudo next
at $
g++ charlie.cpp -o charlie
//that should output a file maybe with diamond icon
next at $:
./charlie
//that should give out put in terminal
If you're interested in C++, I'd recommend using actual C++ in the code, rather than C. Not that there is anything wrong with C. It's just that what you have there is no longer C and it's only loosely C++.
Because you have #include'd cstdio which is a C++ wrapper around the old C input/output functionality - it's technically C++ code, but you're using the C standard library to access i/o functionality. Which is fine, if that's what you want to use.
But also - as it stands - because you've included cstdio and NOT stdio.h - that code will
only compile using a C++ compiler like g++ or clang++. It will no longer compile under a C compiler like gcc, or clang.
[aside]
Regarding Charlie's clang question:
Clang is a C compiler. Clang++ is a C++ compiler. The Clang compilers are an alternative to GNU's gcc/g++ etc. And use the LLVM compiler backend.
[/aside]
Going back to mixing C with C++:
If you use ldd to examine the link dependencies in the version compiled and linked using g++, you'll see something like this:
Code:
$ ldd charlie
linux-vdso.so.1 (0x00007ffd1019e000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fa3e22dc000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fa3e2159000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fa3e213f000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fa3e1f7e000)
/lib64/ld-linux-x86-64.so.2 (0x00007fa3e24b9000)
You can see that there are a lot of dependencies that have been linked in. NOTE - I'm on 64 bit linux!
If you are compiling pure C code - it would be more efficient to compile and link it as C using gcc. Then your executable will only be linked with the standard C library and the ld-linux library.
So if you used pure C, like this:
C:
#include <stdio.h>
int main()
{
printf("Hello my name is charlie.... etc etc.\n");
return 0;
}
You can compile and link the above as pure C using gcc or clang and there will be far less linker dependencies:
Code:
$ ldd charlie
linux-vdso.so.1 (0x00007fff96f8d000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f384d852000)
/lib64/ld-linux-x86-64.so.2 (0x00007f384da6c000)
However - if you want to be able to compile the code in gcc AND g++ - you could simply use the previous pure C snippet and compile it with g++ (or clang++), because any valid C program is also a valid C++ program.
Or alternatively - you could do something with the pre-processor like this:
C:
#ifdef _cplusplus
#include <cstdio>
#else
#include <stdio.h>
#endif
int main()
{
printf("Hello my name is Charlie - etc etc\n");
return 0;
}
Now the code is simultaneously C (using stdio.h) AND is also C++ (but using the C standard library via cstdio). How it's interpreted will depend on which compiler we compile it with.
The code above will (conditionally) be compiled as C using gcc (or clang) or compiled as C++, using g++ (or clang++).
In the above, the preprocessor only includes cstdio if we're compiling with a C++ compiler, otherwise it includes stdio.h if we're compiling using a C compiler.
But as mentioned, the pure C snippet I posted - which only included stdio.h - will compile just fine under a C++ compiler. Because any valid C program will compile as C++.
In a large project - where you might have a lot of legacy C code that is compiled as C alongside more recent code written and compiled as C++ - there are various other techniques you can use, to allow different modules to compile/link and interact cleanly with one another. But that goes way beyond the scope of this thread!
Going back to using C++:
Instead of using legacy C functionality like printf - it would be better to use std::cout, which is in the standard C++ iostream library.
So your code would look more like this:
C++:
#include <iostream>
int main()
{
std::cout << "Hello, my name is Charlie - etc etc....\n";
return 0;
}
And before anybody says anything about adding
using namespace std;
after the includes, so you don't have to type
std::cout
every time you use
cout
- this is considered bad practice as it will bring the entire std:: namespace into your project. (
See here)
Because
std::cout
is only used once in the above - it makes sense to explicitly specify it. If it was to be used multiple times - I would consider adding a more specific "using" declaration like
using std::cout;
after the includes.
Then we could use
cout << "blah blah blah.\n"
instead of
std::cout << "blah blah blah.\n"
each time.
But again, in the above example - it's only used once - so it makes more sense to explicitly specify
std::cout
.
perhaps leave this thread for C++ for those maybe starting out may want to chip in. I noticed
@JasKinasis has a thread on more advanced C++
Over the years, I imagine I've probably answered a few C and C++ related questions here! Sadly, not very many recently though!
Some of them may even have been lost to the sands of time after Rob restarted the site from scratch some years ago!
Is the article in question the one about creating QT based applications without using an IDE like QT Creator? Or a different one?