External functions from standard libraries are unresolved in lli - llvm

I am trying to run simple code in llvm lli (according to Getting Started with the LLVM System)
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
I got the .bc file with
clang –O3 –emit-llvm hello.c –c –o hello.bc
and
lli hello.bc
but getting the next error:
LLVM ERROR: Program used external function '_printf' which could not be resolved!
I think it is very basic error, but I didn't understand what I did wrong.
working on windows.

Related

lli is generating run-time error for clang++ generated IR while the generated executable is not generating run-time error for c++ source code

I am trying to generate a bit code from a c++ source code and running through the just-in-time compiler. When I compile through the clang++ and generate binary executable it runs perfectly but when I generated the bitcode and tried running through the JIT with lli command it generates run-time error. Could you please help me understanding what's going on.
For example: Let example.cpp contains the following code:
#include <iostream>
int main(){
std::cout << "\nHello World!";
return 0;
}
I am using the following command to generate executable which runs perfectly fine.
clang++ example.cpp
I am using the following command to generate the bitcode:
clang++ -S -emit-llvm example.cpp
And then running through the JIT using the following command which generates run-time error:
lli example.ll
I am getting the following access violation error:
Stack dump:
0. Program arguments: lli example.ll
#0 0x00000000025fd9af llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Unix/Signals.inc:564:0
#1 0x00000000025fda42 PrintStackTraceSignalHandler(void*) /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Unix/Signals.inc:625:0
#2 0x00000000025fb7ca llvm::sys::RunSignalHandlers() /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Signals.cpp:68:0
#3 0x00000000025fd329 SignalHandler(int) /home/xpc/llvm/llvm-project1/llvm-project/llvm/lib/Support/Unix/Signals.inc:406:0
#4 0x00007fa75dbdc390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
Segmentation fault (core dumped)
Try compiling with clang++ -S -emit-llvm -fno-use-cxa-atexit example.cpp.
I think it's probably because clang and gcc try prefer __cxa_atexit to
atexit by default (these are functions used for cleaning up global objects when a program exits). Meaning you'll get a linker error if your libc implementation doesn't support the former. So disabling the use-cxa-atexit flag should work.
It seems that it's a library linking issue. Try to use the followings:
Try lli -force-interpreter example.ll instead of lli example.ll
When JIT can't work properly, you should try interpreter.
if the following err:
LLVM ERROR: Could not resolve external global address: __dso_handle
Then add -fno-use-cxa-atexit to clang flags, reasons from [LLVMdev] MCJIT/interpreter and iostream.
After retry lli -force-interpreter example.ll if still have some errors like LLVM ERROR: Tried to execute an unknown external function: .... Then you should recompile LLVM with ffi library enabled. See the following for reasons: Advice on Packaging LLVM¶
and [LLVMdev] lli --force-interpreter does not find external function

ld cannot find /lib64/libmvec.so.1 with g++ command

I'm getting started learning c++ project building on linux with simple hello world code.
#include <iostream>
int main(){
std::cout<<"hello,world"<<std::endl;
return 0;
}
When I try to run command
g++ hello.cpp
It fails with:
/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/ld: cannot find /usr/lib64/libmvec_nonshared.a
/opt/rh/devtoolset-6/root/usr/libexec/gcc/x86_64-redhat-linux/6.3.1/ld: cannot find /lib64/libmvec.so.1
But when I try
gcc -lstdc++ hello.cpp
The compilation is successfully finished and generated executable file a.out.
So what is problem here with g++?

LLVM Clang doesn't find Windows.h

Platform: Windows 10 x64,
IDE: text editor
Hello everyone. I am trying to compile a simple code using the Clang compiler, the problem is that when I include the 'Windows.h' header file, Clang cannot compile the code.
To compile, I use this command: clang++ main.cpp -o test.exe
Here is the code:
#include <iostream>
#include <Windows.h>
using namespace std;
int main() {
cout << GetSystemMetrics(SM_CXSCREEN) << endl;
return 0;
}
However, when I use the mingw compiler, the code compiles correctly. Visual Studio versions '17 and '19 are installed on the systems. I get errors like this when compiling:
main-a354a4.o : error LNK2019: unresolved external symbol __imp_GetSystemMetrics referenced in
function main
test.exe : fatal error LNK1120: 1 unresolved externals
clang++: error: linker command failed with exit code 1120 (use -v to see invocation)
So how to compile this code using Clang?

Getting error clang: error: linker command failed with exit code 1 (use -v to see invocation) while compile C++ file from terminal

I'm getting this error
clang: error: linker command failed with exit code 1 (use -v to see invocation)
while i'm compiling simple cpp file from terminal
gcc hello.cpp
here's the content of hello.cpp file:
#include <iostream>
using namespace std;
// main() is where program execution begins.
int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
I think it might conflict with XCode compiler?
gcc hello.cpp should be g++ hello.cpp
gcc is for compiling and linking C code, while g++ is used for C++ code as you have it.
I think it might conflict with XCode compiler?
No. The point is that the gcc command is also able to compile C++ code detected from the .cpp file extension, though the libstdc++.a won't be linked automatically.

Clang fails to find stdio.h. Which flag should I add?

Here is a simple program
#include <stdio.h>
int main(){
printf("Hello World\n");
return 0;
}
Compiling this program with Clang gives the error below.
$ clang test.cpp
test.cpp:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
^
1 error generated.
The program above is a shortened version from a large project. I can neither change the program, or change the compiler to use, namely clang. So it seems to me the only options is to change environment variables so to alter the flags used by Clang. Which flag should I use?