Part/complete compilation with clang++

Dear List

I have a small program which contains the code shown on page 1058 of
Stroustrup’s TC++PL (4th ed.) just above the 37.2.1 section. I omitted
the line which initialises (and declares) pat1.

If I compile the file main.cpp to regex using this command:
clang++ -g -std=c++11 -stdlib=libc++ -o regex main.cpp
I get no warnings, no errors, in fact no console output from clang++.
When the program is run, it produces “\w+\d*” on the console.

Now, if I compile with this command:
clang++ -g -std=c++11 -o main.o -c main.cpp
and then link with this command:
clang++ -g -stdlib=libc++ -o regex main.o
again, I get no output from clang++. Unfortunately, when the output
program is run, it fails with “Segmentation violation”.

Does this mean that when I start writing programs which are made up
of many source files, I shall be unable to compile the individual files?

I am trying to use Code::Blocks as the IDE for clang++. The commands it
issues are peculiar and, especially, the linker is, I reckon, just plain
wrong. The point is that the Code::Blocks binary also fails with a
Segmentation violation.

So what’s going wrong? Can clang++ act, not only as a compiler, but also a
linker? And is it possible to use a debugger from the LLVM stable rather than
GNU gdb?

Looking forward to progessing on these points.

Dear List

I have a small program which contains the code shown on page 1058 of
Stroustrup's TC++PL (4th ed.) just above the 37.2.1 section. I omitted
the line which initialises (and declares) pat1.

Not everyone has a copy of TC++PL on hand, perhaps you could provide
the code you are compiling?

If I compile the file main.cpp to regex using this command:
   clang++ -g -std=c++11 -stdlib=libc++ -o regex main.cpp
I get no warnings, no errors, in fact no console output from clang++.
When the program is run, it produces "\w+\d*" on the console.

If you avoid initializing a variable you may get undefined behavior,
which isn't going to be reliable.

Now, if I compile with this command:
  clang++ -g -std=c++11 -o main.o -c main.cpp
and then link with this command:
  clang++ -g -stdlib=libc++ -o regex main.o
again, I get no output from clang++. Unfortunately, when the output
program is run, it fails with "Segmentation violation".

Again, I'm assuming you've invoked UB & the two different standard
libraries are just failing in different ways. Once we see the code,
that might change.

Does this mean that when I start writing programs which are made up
of many source files, I shall be unable to compile the individual files?

I don't understand this question, well maybe - no, there shouldn't
really be any difference between compiling and then linking and
compiling+linking in one go. But UB is UB (though I'd still be
surprised if that actually behaves differently)

I am trying to use Code::Blocks as the IDE for clang++. The commands it
issues are peculiar and, especially, the linker is, I reckon, just plain
wrong. The point is that the Code::Blocks binary also fails with a
Segmentation violation.

So what's going wrong? Can clang++ act, not only as a compiler, but also a
linker?

Both GCC and Clang (& many other compilers) can act as wrappers around
assemblers and linkers. They just invoke the assembler (well,
Clang/LLVM has an integrated assembler too, so it doesn't invoke
another process to assemble) or linker with the relevant arguments
(such as figuring out the right libraries to link for the standard
library, etc)

And is it possible to use a debugger from the LLVM stable rather
than
GNU gdb?

There is a debugger under the LLVM project, called LLDB - it's not
very mature & even less so on Linux. Clang's debug info should operate
perfectly well with GDB, if it doesn't, please file bugs.

http://paste.debian.net/17301

Nothing obviously wrong with that - yes, jumping in with a debugger
might be an option, but the code looks simple enough that I'd wonder
if it's an issue with linking or the standard library.

What does “UB” mean?

Nothing obviously wrong with that - yes, jumping in with a debugger
might be an option, but the code looks simple enough that I'd wonder
if it's an issue with linking or the standard library.

Dear List

If I compile the file main.cpp to regex using this command:
   clang++ -g -std=c++11 -stdlib=libc++ -o regex main.cpp
I get no warnings, no errors, in fact no console output from clang++.
When the program is run, it produces "\w+\d*" on the console.

Now, if I compile with this command:
  clang++ -g -std=c++11 -o main.o -c main.cpp
and then link with this command:
  clang++ -g -stdlib=libc++ -o regex main.o
again, I get no output from clang++. Unfortunately, when the output
program is run, it fails with "Segmentation violation".

Again, I'm assuming you've invoked UB & the two different standard
libraries are just failing in different ways. Once we see the code,
that might change.

What does "UB" mean?

Undefined Behavior ( a good read on the subject if Chris Lattner's 3
part series: http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
) - though from your example there's nothing obviously wrong that I
can see.

The code was lifted from Bjarne’s book, so I’d be surprised if there was anything wrong with it. Because I succeeded in compiling LLVM/Clang using the GNU C++ library, I am now trying to build LLVM/Clang (svn rev. 186765) using the already compiled Clang++ and libc++. We shall see if that succeeds.