I use Ubuntu 14 and try to compile a c++ program in the terminal. Until now I used g++ and compiling worked without problems. As I prefer the error messages from clang I want to work with clang++ from now on.
So far I used the command
g++ -oexec main.cpp file.cpp
but when I try
clang++ -oexec main.cpp file.cpp
I get the error
clang: error: cannot specify -o when generating multiple output files
It works for me. I think you are also passing '-c' flag to the compiler while compiling with clang.
When you pass '-c', it will not work either with clang/gcc. This is because when you pass '-c' you are essentially saying that compile each file into object file (.o files), hence, providing '-o' in that case is incorrect.
This is a list of all Clang flags. As you can see "oexec" is not one of them. Instead Clang is reading your command as -o (Write output to ) and spits out the error, because you can't use -o when outputting to multiple files.
Related
I am new to the clang++ compiler flags. I have an issue regarding compilation. Here is my cmd:
clang++ -I ../llvm-project/llvm/include -I ../llvm-project/clang/include
-I ../llvm-project/build/tools/clang/include -I ../llvm-project/build/include
-O3 -c $(llvm-config-7 --cxxflags)
projectToTestHeadersBuilding.cpp -o projectToTestHeadersBuilding
I am getting error:
./projectToTestHeadersBuilding: cannot execute binary file: Exec format error
After execution I have projectToTestHeadersBuilding file. But I can not run executable. Can you please help me to understand how to get executable, so I can run it using ./projectToTestHeadersBuilding ?
In your initial command you use the -c flag which makes clang output an object file. This is part of a compiled program but not a complete executable, in order to get the final executable you must perform a linking step, usually with other object files.
A simple compilation can be done as so:
clang++ projectToTestHeadersBuilding.cpp -c -o projectToTestHeadersBuilding.o
clang++ projectToTestHeadersBuilding.o -o projectToTestHeadersBuilding
./projectToTestHeadersBuilding
Generally we do not need to explicitly pass all those -I flags you have passed. If they are needed with your setup, add them to the commands I've included above.
Whenever I try to compile a c++ code with g++ on terminal, I can't seem to get g++ to output any error messages even though I am certain that there are errors that should be caught during compile time (such as syntax, reference types...).
I tried several ways such as this make file:
all:
g++ -W -Wall -Werror main.cpp
All it does is output:
make: *** [all] Error 1
which isn't that useful, obviously...
Typing things like this:
g++ -W -Wall -Werror main.cpp
directly to terminal (without the make file) doesn't output any messages at all.
However this successfully outputs all of the errors while compiling:
cc main.cpp
My question is: how do I make g++ to output error messages so I can know where to correct my code?
Just guessing - is it possible your terminal doesn't print stderr? Say, for example, it moves it to a log file or something?
Try running
g++ [whatever your arguments are] |& cat
(this is if you use tcsh)
or
g++ [whatever] 2>&1 | cat
if you use bash.
Try something simple like this:
g++ -c main.cpp
Make reports an error when one of it's tasks returns non-0 status. If g++ silently returns non-0 - well, i suppose it's broken somehow. Check $? after you've run g++. Also, try g++ --version - will it report anything at all? Also you could run it under debugger, just to be sure.
Try adding a line like
#warning hello from here
(or perhaps #error instead of #warning) into main.cc near the beginning (perhaps as the first line).
If
gcc -Wall -v main.cc
don't give any output (notably no warnings or errors) that means that your gcc is broken. Perhaps type /usr/bin/gcc instead of just gcc
BTW, Apple don't like GCC (because they dont like its GPLv3+ license). Maybe it is worth your time to build [using e.g. ..../configure --program-suffix=-local] and install a newer GCC (perhaps from the released source tar ball of the compiler). Current version is 4.8.1!
Summary: llvm-ld has been removed from the LLVM 3.2 release. I am trying to figure out how to use clang in its place in my build system.
Note that I figured out the answer to my own question while writing it but I am still posting it in case it is useful to anyone else. Alternative answers are also welcome.
Details:
I have a build process which first generates bitcode using clang++ -emit-llvm. Then I take the bitcode files and link them together with llvm-link. Then I apply some standard optimization passes with opt. Then I apply another custom compiler pass with opt. Then I apply the standard optimization passes again using opt a third time. Finally I take the output from the last run of opt and use llvm-link to link with appropriate libraries to generate my executable. When I tried to replace llvm-link with clang++ in this process I get the error message: file not recognized: File format not recognized
To make this question more concrete I created a simplified example of what I am trying to do. First there are two files that I want to compile and link together
test1.cpp:
#include <stdio.h>
int getNum();
int main()
{
int value = getNum();
printf("value is %d\n", value);
return 0;
}
test2.cpp
int getNum()
{
return 5;
}
I executed the following sequence of commands:
clang++ -emit-llvm -c test1.cpp test2.cpp
llvm-link -o test.bc1 test1.o test2.o
opt test.bc1 -o test.bc2 -std-compile-opts
(Note that I am currently running llvm 3.1, but I'm trying to figure out the steps that will work for llvm 3.2. I assume that I should be able to make the LLVM 3.1 version work correctly using clang instead of llvm-ld)
Then if I run:
llvm-ld test.bc2 -o a.out -native
everything is fine and a.out prints out 5.
However, if I run:
clang++ test.bc2 -o a.out
Then I get the error message:
test.bc2: file not recognized: File format not recognized clang-3:
error: linker command failed with exit code 1 (use -v to see invocation)
Obviously I know that I can produce an executable file by running clang directly on the .cpp files. But I'm wondering what the best way to integrate clang with opt is.
The test case described in the question can be compiled using the following steps:
clang++ -emit-llvm -c test1.cpp test2.cpp
llvm-link -o test.bc1 test1.o test2.o
opt test.bc1 -o test.bc2 -std-compile-opts
llc -filetype=obj test.bc2 -o test.o
clang++ test.o
This produces a working a.out file.
It seems that llc is needed to convert from bitcode to machine code which can then be processed by clang as it normally would.
In general I've found that
llvm-ld x.bc y.bc
can be replaced with
llc x.bc
llc y.bc
clang x.s y.s
In linux : C++
Is there any way is ther to create a ".O" file without main() function in ".C" file .
iam using
"cc -c file.c -o file.o " with sun compiler
" gcc -Wall -c file.c " with g++ compiler
may i know
1) what command we need to use for with EDG compiler and any other C++ compielrs in LINUX
2) In windows may i know for any compilers ..even g++,cc,intel,etc ...?
please ...
i have seen this question ?
Possible to compile any .c file in isolation (that is, without a main?)
but i didn't find the answer ...
For almost any compiler, -cshould produce a .o file by default. You can use the same syntax as with your sun compiler : -o is supported as well by gcc.
This should work just fine:
gcc -Wall -c file.c -o file.o
Note that on windows, .ofiles are usually named .obj as a visual convention : it might be a good idea to stick to that convention.
I think that if suing visual studio compiler in command line, you must use /c to make it compile, as defined on MS web site
Last, if you're compiling C++, you should use g++ instead of gcc afaik.
gcc -c file.c should work just fine?
Every C compiler I am aware of recognizes the -c flag for this mode of compilation.
I am very aware of compiling C++ programs with g++ in linux environment. But, may be I am missing something, I am getting this strange output/behaviour.
I have source file in test.cpp.
To compile this, I did
(1)
g++ -c test.cpp
g++ -o test test.o
./test
Everything works fine.
But when I did compling and linking in same stage, like this
(2)
g++ test.cpp -o test
./test => Works fine
(3)
g++ -c test.cpp -o test => Doesn't work
In my last case, test is generated but is no more executable; but in my guess it should work fine.
So, what is wrong or do I need to change some settings/configuration ??
I am using g++ 4.3.3
Thanks.
When you say:
g++ -c test.cpp -o test
The -c flag inhibits linking, so no executable is produced - you are renaming the .o file.
Basically, don't do that.
You are forcing compiler to produce an object file and name it like an executable.
Essentially your last line tells: compile this to an object file, but name it test, instead of test.obj.
-c flag means Compile Only
Try
g++ -o test test.cpp
Specifying -o in the g++ command line tells the compiler what name to give the output file. When you tried to do it all in one line, you just told the compiler to compile test.cpp as an object file named test, and no linking was done.
Have a look at the fabulous online manual for GCC for more details.
from the gcc manual:
-c Compile or assemble the source files, but do not link. The linking
stage simply is not done. The ultimate output is in the form of an
object file for each source file.
You must link the compiled object files to get the executable file.
More info about compiling and linking and stuff is here.
Read man g++. The switch -c is to compile only but not to link.
g++ -c test.cpp -o test
does what
g++ -c test.cpp
does but the object file will be test istead of the default name test.o. An object file cannot be executed.