C compile c++ code with clang

Hello guys,

can you tell me please how to compile c++ code with clang compiler ?

Very simple example. I've create the file test.cpp.


C++:
#include <iostream>
main()
{
    cout << "Hello baby!";
}

Compilation: clang test.cpp gves me error on the iostream class.

Tips are welcome.
 
Code:
$ clang /tmp/test.cpp
/tmp/test.cpp:2:1: error: C++ requires a type specifier for all declarations
main()
^
/tmp/test.cpp:4:5: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
    cout << "Hello baby!";
    ^~~~
    std::cout
/usr/include/c++/v1/iostream:54:33: note: 'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
                                ^
2 errors generated.
I do not mean to be sarcastic, but read the error messages and do what they suggest.

If you don't understand the error messages (what's a type specifier?), find a good book. Personally I recommend "The C++ Programming Language" by Bjarne Stroustrup. I'm sure others will have different suggestions.

You'll get much further if you understand the language rather than asking for hand-holding. Sorry if that sounds harsh.
 
Exactly the same:
Code:
$ clang++ /tmp/test.cpp
/tmp/test.cpp:2:1: error: C++ requires a type specifier for all declarations
main()
^
/tmp/test.cpp:4:5: error: use of undeclared identifier 'cout'; did you mean 'std::cout'?
    cout << "Hello baby!";
    ^~~~
    std::cout
/usr/include/c++/v1/iostream:54:33: note: 'std::cout' declared here
extern _LIBCPP_FUNC_VIS ostream cout;
                                ^
2 errors generated.
 
Exactly the same:


Ops .... I forgot a line (using namespace std) .....

C++:
#include <iostream>

main()
{
    cout << "Hello baby!";
}

... and the correct one:

C++:
#include <iostream>

using namespace std;

main()
{
    cout << "Hello baby!";
}

Btw ... can you tell me if working with the clang compiler, c code has to be compiled with the command clang and c++ code has to be compiled with clang++.

Using clang++ I've not received errors.
Thanks.
 
That's still invalid code.
Code:
$ clang++ /tmp/test2.cpp
/tmp/test2.cpp:7:1: error: C++ requires a type specifier for all declarations
main()
^
1 error generated.

The only difference between clang and clang++ is the library used to link against, clang links with stdc while clang++ links with stdc++.

clang++ test.cpp
is equivalent to
clang test.cpp -lstdc++

The compiler is the same binary, it just depends on how it's invoked.

Code:
$ md5 `which clang` `which clang++`
MD5 (/usr/bin/clang) = e8c3e6bd8c0f422050ebab82693023fb
MD5 (/usr/bin/clang++) = e8c3e6bd8c0f422050ebab82693023fb
 
Yes it's clear ... main require the 'int' specifier.
Your problems are much bigger than "main" requiring "int".

All functions have return types. Including main, which is just a function.
All functions have arguments. It is bad style to leave the argument empty; better to make it function(void).

By the way, the arguments to main() are standardized. I typically use int main(int argc, char* argv[]), but there are other options; some people prefer "char** argv", which is also correct.

You should also go and understand the special rule about returning 0 in main. Relying on that rule is considered bad style though; I would always put "return 0;" as the last line of main.

If you want to program C++, you better understand namespaces, really well. That's not a line one "forgets" ... it either means you don't understand the language, or you didn't design your code.

And finally, none of the questions you are asking here are clang (or clang++) specific. You would get the same problems and solutions with gcc, the AIX or Solaris compilers, and with Windows C++.

The level of your questions show that you are trying to program in C++, without having learned enough about it. To be blunt: You urgently need to spend two evenings reading a C++ textbook, before you touch the keyboard again. Someone suggested the Stroustrup book above; that is a very good book, and way too fat. However, it is not easy to understand. Personally, I prefer Stan Lippman's "C++ primer", which is better at teaching things, while Stroustrup is better at confusing people and being a language lawyer. Bjarne Stroustrup has recently written a much thinner book called "A tour of C++", which is a pretty good introduction, and thankfully very brief.
 
Back
Top