I'm trying to use the C++ compiler to compile the following program:
#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Module.h"
int main( int argc, char* argv[] )
{
if( argc < 2 )
llvm::errs() << "Expected an argument - IR file name\n";
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::SMDiagnostic err;
llvm::Module* module = llvm::ParseIRFile( argv[1], err, context );
if( !mod )
{
err.print( argv[0], errs() );
return 1;
}
return 0;
}
I'm trying to compile the program using the following command:
clang++ main.cpp -o main
However, when I compile, I'm getting the following compile error:
main.cpp:2:10: fatal error: 'llvm/IR/LLVMContext.h' file not found
#include "llvm/IR/LLVMContext.h"
^
1 error generated.
In this case, I'm not exactly sure how to link the LLVM API headers when compiling main.cpp with Clang.
Any help would be greatly appreciated.
You can use the following command:
g++ -std=c++11 main.cpp `llvm-config --system-libs --cppflags --ldflags --libs core` -o main
Where --libs and --system-libs flags are used for linking and --cppflags takes care of include paths.
Thank You
You need LLVM checked out or installed somewhere on your system. You can download a binary release (with headers and libraries you can build against) as explained here: http://llvm.org/releases/download.html#3.5
You can also check out LLVM from its SVN repository as explained here: http://llvm.org/docs/GettingStarted.html#checkout
Once you do that, I recommend looking at the llvm-clang-samples repository that comes with a Makefiles showing how to build sample programs vs. an up-to-date LLVM.
Related
I'm trying to link OpenBLAS library with MinGW w64 compiler on Windows.
This is my code:
#include <cstdio>
#include <cblas.h>
#include <cstdlib>
int main(){
double m[10],n[10];
int i, result;
for(i=0;i<10;i++)
m[i] = 1.0l*rand()/RAND_MAX;
for(i=0;i<10;i++)
n[i] = 1.0l*rand()/RAND_MAX;
result = cblas_ddot(10, m, 1, n, 1);
return 0;
}
and compiling with this command:
g++ ^ -IC:\OpenBLAS-0.3.6-x64\include -LC:\OpenBLAS-0.3.6-x64\lib -lopenblas blas.cpp
and get an error
undefined reference to `cblas_ddot'
I downloaded precompiled binaries from here and using 64bit Windows, g++ (x86_64-win32-seh-rev0, Built by MinGW-W64 project) 8.1.0
How can I fix this error?
A general suggestion is to always put source and object files before linked libraries.
In general not all of the library functions are used, but only the ones needed by the main source of code. Then the linker needs to know the undefined symbols before looking into the libraries.
Then putting blas.cpp before -lopenblas should work.
g++ ^ -IC:\OpenBLAS-0.3.6-x64\include -LC:\OpenBLAS-0.3.6-x64\lib blas.cpp -lopenblas
I have simple source code, that includes gtest and launches testing. Here are the file tests.c:
#include <gtest/gtest.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf(" init GTest ");
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I launched build using compiler gcc:
gcc -o MY_UNIT_TESTS tests.c -I/usr/include /usr/lib/libgtest.a /usr/lib/libgtest_main.a -lpthread
And it is failed with this error:
In file included from tests.c:1:0:
/usr/include/gtest/gtest.h:54:18: fatal error: limits: No such file or directory
compilation terminated.
If I use compiler g++, everything is ok. I need to test library, that was built using gcc compiler, so my test should be also build with gcc.
How can I build this code using gcc compiler ?
Google Test is written in C++ (and your are tests too) and you need to use g++ to compile your tests. You can build your library with gcc and you can call your C code from your C++ tests, but there are some things to be aware of.
Hi am trying to compile a src file by linking an external library.
Getting below error
undefined reference to `__isoc99_sscanf#GLIBC_2.7'
make -f GNUmakefile
g++ -m32 -D_POSIX_PTHREAD_SEMANTICS -g -Wl,--version-script=fix.txt -D_GNU_SOURCE -I../include ConnectionAndAuthExample.cpp -o ../Linux/ConnectionAndAuthExample_32 -L../Linux -lsomelib
../Linux/libsomelib.so: undefined reference to `__isoc99_sscanf#GLIBC_2.7'
Contents of fix.txt are
GLIBC_2.7 {
global: *;
local: *;
};
Similar query was answered below here doesnt help. I want to know if anyone else have encountered similar error and resolved it.
My GCC version
-bash-3.2$ g++ --version
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-50)
Finally the link in the question help with slight modification from other answers. Here is my fix.
I created a new cpp file with below contents
#include <iostream>
#include <stdarg.h>
__asm__(".symver __isoc99_sscanf,__isoc99_sscanf#GLIBC_2.7");
#ifdef __cplusplus
extern "C" {
#endif
int __isoc99_sscanf(const char *a, const char *b, va_list args)
{
int i;
va_list ap;
va_copy(ap,args);
i=sscanf(a,b,ap);
va_end(ap);
return i;
}
#ifdef __cplusplus
}
#endif
Compiled it to get the object file and used it along with fix.txt mentioned in the question.
However, I would like to inform that since my third party library was compiled on an higher version. I am getting ELF file OS ABI invalid when I run my binary.
There solution from some other answer which suggests to use patchelf and modify intert section doesnt help as shared files mostly dont have that section.
I also tried to compile glibc2.7 and build my binary using
-Wl,--dynamic-linker
-Wl,--rpath
flags pointing to glibc2.7 . Still in vain.
I use MinGW on windows7 32bit.
And I can’t compile my source which uses pthread.
My code is below.
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
}
Error happens as I compile it.
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
C:\Users\xxx\AppData\Local\Temp\cc9OVt5b.o:trylpthread.c:(.text+0x25): undefined reference to `_imp__pthread_create'
collect2.exe: error: ld returned 1 exit status
I use following pthread library.
pthreads-w32-2.8.0-3-mingw32-dev
And here is libpthread.dll.a in /usr/local/lib
Does anyone know how to fix this problem?
The commandline:
gcc -I /usr/local/include -L /usr/local/lib/libpthread.dll.a trylpthread.c
does not make sense.
-L <dir> is a linker option that directs the linker to search for required libraries
in directory <dir>. Thus you are telling the linker to search for required libraries in
path /usr/local/lib/libpthread.dll.a, which is not a directory, while on the other hand
you are not telling the linker to link any libraries at all. That is why it fails to find any
definition for _imp__pthread_create.
Neither does the program you have posted make sense. The lines:
#ifndef pthread_create
return ((int*)(&pthread_create))[argc];
#else
(void)argc;
return 0;
#endif
say:-
If I have not defined a preprocessor macro pthread_create then compile:
return ((int*)(&pthread_create))[argc];
else compile:
(void)argc;
return 0;
Well if you had defined a preprocessor macro pthread_create, e.g.
#define pthread_create whatever
then the code you would compile would be:
(void)argc;
return 0;
And since you have indeed not defined any such macro, the code you compile is:
return ((int*)(&pthread_create))[argc];
which fails at linkage, as you see. And if that code was compiled with pthread_create so defined,
it would be:
return ((int*)(&whatever))[argc];
Rewrite your program as:
#include <stdio.h>
#include <pthread.h>
int main(int argc, char** argv)
{
(void)argv;
printf("######## start \n");
return ((int*)(&pthread_create))[argc];
}
Compile with:
gcc -Wall -I /usr/local/include -o trylpthread.o -c trylpthread.c
Link with:
gcc -o trylpthread.exe trylpthread.o /usr/local/lib/libpthread.dll.a
Remember that when you get the program compiled and linked, the appropriate pthreadGC??.dll
must be found at runtime in one of the places where the program loader searches for dlls.
Better still, uninstall your MinGW and your pthreads-w32-2.8.0-3-mingw32-dev and
install a more up-to-date Windows port of GCC, e.g. TDM-GCC (simplest) or mingw-w64. Pick the 32-bit version, if your Windows system
is 32-bit. These toolchains come with built in pthread support, as GCC standardly does.
Compile with:
gcc -Wall -o trylpthread.o -c trylpthread.c
Link with:
gcc -o trylpthread.exe trylpthread.o -pthread
(not -lpthread)
I wrote up a simple C++ program that relies on forward_list like
#include <forward_list>
#include <iostream>
int main() {
std::forward_list<int> my_list;
my_list.push_front(3);
std::cout << my_list.top() << std::endl;
return 0;
}
However, when I compile this program on my Mac with clang++ my_program.cpp -std=c++11 -o my_program, I get this error:
my_program.cpp:1:14: fatal error: 'forward_list' file not found
#include <forward_list>
^
1 error generated.
Why can't clang find forward_list? Other C++11 features are working. For instance, the auto keyword works, albeit a warning appears that tells me that auto is a C++11 feature.
By default clang++ uses an older gcc-4.2 std library which has no C++11 support. You can tell clang to use a C++11-aware std::lib with the command -stdlib=libc++. libc++ has <forward_list>.