1

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 ?

Louen
  • 3,617
  • 1
  • 29
  • 49
Farrukh Nabiyev
  • 346
  • 2
  • 11
  • Does this answer your question? [how to run the .o file after make](https://stackoverflow.com/questions/22251571/how-to-run-the-o-file-after-make) – Ranoiaetep Oct 21 '21 at 17:32
  • Hello, no it does not. I don't have an executable. I am only getting .o file. -bash: ./projectToTestHeadersBuilding: cannot execute binary file: Exec format error – Farrukh Nabiyev Oct 21 '21 at 17:36
  • 1
    The `-c` file specifies clang should compile a shared object library and not an executable. You are compiling a shared object. Shared objects are not executable. – h0r53 Oct 21 '21 at 18:08
  • 3
    @h0r53 - `-c` means you get an object, not a shared object. – Stephen Newell Oct 21 '21 at 18:29
  • 1
    That link actually does answer your question, you'll just to read all of the answers. Also, those flags are not exclusive to clang. You'd run into this issue with gcc as well. – sweenish Oct 21 '21 at 19:38

1 Answers1

4

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.

qz-
  • 674
  • 1
  • 4
  • 14
  • Can you please specify how I should add -I and -O3 -c $(llvm-config-7 --cxxflags) to the execution line? Because it gives me a bunch of errors if I delete them – Farrukh Nabiyev Oct 22 '21 at 12:00
  • 1
    @FarrukhNabiyev Just add them at the end. – qz- Oct 22 '21 at 13:53
  • Thank you. But I do not get projectToTestHeadersBuilding file after running this command. I get warnings and two files projectToTestHeadersBuilding.cpp and projectToTestHeadersBuilding.o – Farrukh Nabiyev Oct 22 '21 at 14:06
  • Run the second command – qz- Oct 22 '21 at 14:12
  • It gives an error: `projectToTestHeadersBuilding.cpp:(.text+0x2b): undefined reference to clang_getCursorKind` `projectToTestHeadersBuilding.cpp:(.text+0x352): undefined reference to clang_createIndex` – Farrukh Nabiyev Oct 22 '21 at 14:20
  • 1
    The problem is in some libraries, thank you. I have compiled it successfully – Farrukh Nabiyev Oct 22 '21 at 14:25