bc file only with llvm-gcc3?

question about:
http://llvm.org/docs/FAQ.html#translatec++
"With llvm-gcc3, this will generate program and program.bc."

Is this llvm-gcc3 only out of date of is it really only llvm-gcc3?

I have 'llvm-g++ (GCC) 4.2.1 (Based on Apple Inc. build 5546) (LLVM
build)' installed on Ubuntu and followed that guide and it creates
indeed no .bc file.

Regards,
-mr

Those instructions are out of date; the current version of llvm-gcc
doesn't generate bitcode files unless you explicitly ask it to
generate them. The correct procedure with a current LLVM and llvm-gcc
is something like the following:

llvm-g++ a.cpp -c -emit-llvm
llvm-g++ b.cpp -c -emit-llvm
llvm-link a.bc b.bc -o program.bc
llc -march=c program.bc -o program.c
gcc program.c

-Eli

Eli Friedman schrieb:

question about:
http://llvm.org/docs/FAQ.html#translatec++
"With llvm-gcc3, this will generate program and program.bc."

Is this llvm-gcc3 only out of date of is it really only llvm-gcc3?

Those instructions are out of date; the current version of llvm-gcc
doesn't generate bitcode files unless you explicitly ask it to
generate them. The correct procedure with a current LLVM and llvm-gcc
is something like the following:

llvm-g++ a.cpp -c -emit-llvm
llvm-g++ b.cpp -c -emit-llvm
llvm-link a.bc b.bc -o program.bc
llc -march=c program.bc -o program.c
gcc program.c

-Eli

Thanks for fast response!
Currently I am trying what you told me.

No matter if I try "
llvm-g++ a.cpp -c -emit-llvm
" with "LLVM-GCC 4.2 Front End Binaries for Mingw32/x86" on Windows XP
or if I am trying it on Ubuntu...

Only the a.o file will be created without any error messages. The .bc
file will not be created, do you know why?

Regards,
-mr

-emit-llvm doesn't change the default name for the -c output.
The .o file should be llvm binary IR; if you want it named .bc, use -o a.bc
If you're starting out, probably -S -emit-llvm is more useful, you can read that format.

Dale Johannesen schrieb:

No matter if I try "
llvm-g++ a.cpp -c -emit-llvm
" with "LLVM-GCC 4.2 Front End Binaries for Mingw32/x86" on Windows XP
or if I am trying it on Ubuntu...

Only the a.o file will be created without any error messages. The .bc
file will not be created, do you know why?

-emit-llvm doesn't change the default name for the -c output.
The .o file should be llvm binary IR; if you want it named .bc, use -o
a.bc
If you're starting out, probably -S -emit-llvm is more useful, you can
read that format.

Ok, I understand so far.

I have a partially working example now.

llvm-g++ a.cpp -c -emit-llvm
llvm-g++ b.cpp -c -emit-llvm
llvm-link a.o b.o -o program.bc
llc -march=c program.bc -o program.c
gcc program.c

program.c will contain some non-human generated code. But it doesn't
compile with gcc. There is an error, gcc complaints about undefined
reference to std::cout and such (the code was just a very small example
with cout).

It hasn't be converted into "C only" code, can you tell me why?

Regards,
-mr

Hello Michael,

If you're just trying to run the code you can skip the llc and gcc states and go directly to lli. PowerPC and Intel x86 are the only architectures supported by the jit compiler right now but it should work fine from Windows. The C output backend needs some work since C isn't a low-level language enough to compile the abstractions of LLVM Assembly code.

--Sam Crow

If your code uses the C++ library, you'll need to link it against a
C++ library. That has nothing to do with the language of the code.

-Eli

Nope, I planed to convert into C code, compilable with a C compiler.

-mr

Samuel Crow schrieb:

Eli Friedman schrieb:

program.c will contain some non-human generated code. But it doesn't
compile with gcc. There is an error, gcc complaints about undefined
reference to std::cout and such (the code was just a very small example
with cout).

It hasn't be converted into "C only" code, can you tell me why?

If your code uses the C++ library, you'll need to link it against a
C++ library. That has nothing to do with the language of the code.

-Eli

I was only using the C++ standard lib. It doesn't need to be linked
because it's only a header. Better said, it can't be linked with the std
lib, never heard that it's needed to link it.

Btw the code compiles and runs for sure with a g++ and also with llvm-g++.

Only the converted C output contains still references to C++. How can I
remove them?

-mr

I was only using the C++ standard lib. It doesn't need to be linked
because it's only a header. Better said, it can't be linked with the std
lib, never heard that it's needed to link it.

Your program does need to have libstdc++ to linked it.

Btw the code compiles and runs for sure with a g++ and also with llvm-g++.

Right, because they provide all needed extra magic libraries to linker.
This will surely fail for you:

g++ foo.cpp -o foo.o
gcc foo.o

Only the converted C output contains still references to C++. How can I
remove them?

You cannot, since you're compiling C++ sources and using C++ standard
library. You've mentioned this explicitly before:

There is an error, gcc complaints about undefined
reference to std::cout and such (the code was just a very small
example with cout).

Once again: you have C source code but with calls to C++ standard
library (just as normal C calls). Thus you need to link libstdc++ in!

Please bare with me. :slight_smile: Just to ensure I got nothing wrong.

I can write in C++ + use C++ std lib + use other C++ libs and see if it
compiles with a C++ compiler.

After it's working I use llvm to do some magic and I get C code that I
can compile with a standard C compiler on any platform? That's what I
planed to do.

Regards,
-mr

Michael Reichenbach wrote:

Please bare with me. :slight_smile: Just to ensure I got nothing wrong.

I can write in C++ + use C++ std lib + use other C++ libs and see if it
compiles with a C++ compiler.

After it's working I use llvm to do some magic and I get C code that I
can compile with a standard C compiler on any platform? That's what I
planed to do.

It's not the compiler that's causing the problem. All of the C++-isms are getting converted into C, which you can then compile with a C compiler. This includes things like new/delete, operator overloading, etc.

The problem is that you are using code found in the standard C++ library which the linker can't find at link time. When you use the g++ driver to do linking, it knows about the standard library and "does the right thing" during linking. If you use the system linker you need to tell it where to find the standard C++ library. You do this in LLVM by passing -lstdc++ to llvm-ld during linking.

So the answer to your question is "yes" you can generate C code that will compile on any platform, but in order to link it correctly you need to link in the C++ library on that platform.

Hope this helps.

Luke

Hello, Michael

After it's working I use llvm to do some magic and I get C code that I
can compile with a standard C compiler on any platform? That's what I
planed to do.

Luke already answered one part of your question. Another part is that
you cannot run resulting C code on *any* platform: inside bytecode you
will have all sort of frontend-related stuff encoded implicitely: sizes
of integers, endian-related things, struct layout, etc. LLVM IR obtained
from C/C++ is not portable.