Compile a program with using clang API - c++

I built llvm with clang on Windows using mingw (Windows 8 64-bit, MinGW 20120426):
./configure --disable-docs --enable-optimized --enable-targets=x86,x86_64 && make && make install
Build success.
Now I want compile simple program using clang-c API:
#include <clang-c/Index.h>
int main (int argc, char** argv) {
CXIndex index = clang_createIndex (false,true);
}
Run gcc:
gcc 1.cpp -IC:/MinGW/msys/1.0/local/include -LC:/MinGW/msys/1.0/local/lib -lclang -lstdc++
Compile ok, but link failed:
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0xe3): undefined reference to `llvm::sys::MutexImpl::~MutexImpl()'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x11f): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x168): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x333): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x394): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x435): undefined reference to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x464): more undefined references to `clang::SourceManager::isBeforeInTranslationUnit(clang::SourceLocation, clang::SourceLocation) const' follow
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x476): undefined reference to `clang::SourceManager::getMacroArgExpandedLocation(clang::SourceLocation) const'
C:/MinGW/msys/1.0/local/lib/libclang.a(CIndex.o):CIndex.cpp:(.text+0x4e7): undefined reference to `clang::ASTUnit::Save(llvm::StringRef)'
...
over 700 lines.
$ llvm-config.exe --version
3.1
$ llvm-config.exe --prefix
C:/MinGW/msys/1.0/local

SOLUTION
Add all params from llvm-config --cxxflags --ldflags --libs all
I write makefile like this
CXX := g++
LLVMCOMPONENTS := all
RTTIFLAG := -fno-rtti
LLVMCONFIG := llvm-config
CXXFLAGS := $(shell $(LLVMCONFIG) --cxxflags) $(RTTIFLAG)
LLVMLDFLAGS := $(shell $(LLVMCONFIG) --ldflags --libs $(LLVMCOMPONENTS))
SOURCES = 1.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXES = $(OBJECTS:.o=)
CLANGLIBS = \
-lclang\
-lclangTooling\
-lclangFrontendTool\
-lclangFrontend\
-lclangDriver\
-lclangSerialization\
-lclangCodeGen\
-lclangParse\
-lclangSema\
-lclangStaticAnalyzerFrontend\
-lclangStaticAnalyzerCheckers\
-lclangStaticAnalyzerCore\
-lclangAnalysis\
-lclangARCMigrate\
-lclangEdit\
-lclangAST\
-lclangLex\
-lclangBasic\
$(shell $(LLVMCONFIG) --libs)
all: $(OBJECTS) $(EXES)
%: %.o
$(CXX) -o $# $< $(CLANGLIBS) $(LLVMLDFLAGS)
Complile and link success.

Related

Can't figure out how to properly format Makefile using SDL2. Undefined reference

Makefile:
OBJS = Instantiation
Exec_NAME = Test.exe
CC = g++ #Compiler name
COMPILER_FLAGS = -c -g -Wall -std=c++11
#Issue exists somewhere in these next 3 lines, but what, how, why :( ?
INC = -I/SDLDEPS/include
LIB = -L/SDLDEPS/lib
LINKER_FLAGS = -lmingw32 -lSDL2main -lSDL2 #just saw this a lot, not sure what the mingw32 is.
#inclusion of $(INC) $(LIB) $(LINKER_FLAGS) not right
all: $(Exec_NAME)
$(Exec_NAME): $(OBJS).o
$(CC) -o $(Exec_NAME) $(OBJS).o
Instantiation.o: $(OBJS).cpp
$(CC) $(COMPILER_FLAGS) $(INC) $(LIB) $(LINKER_FLAGS) $(OBJS).cpp
clean:
rm -f $(Exec_NAME) $(OBJS).o
rebuild:
make clean
make
//Main.cpp
#include <iostream>
#include "Window.h"
//#include "SDLDeps/include/SDL.h"
#include "SDLDeps/include/SDL.h"
int main(int argc, char** argv){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
std::cerr << "SDL Failed to initalize\n";
else
std::cout << "Great Success";
Window window("TEST WINDOW");
while(!window.isClosed()){
window.pollEvents();
window.Clear();
}
//std::string string = "";
//std::cout << "HELLO PERSON What is your name?\n";
//std::cin >> string;
//std::cout << string << "!\n";
return 0;
}
//Window.h I don't think Window.cpp is necessary as the issue is linking, I think.
#ifndef WINDOW_H
#define WINDOW_H
#include <string>
#include "SDLDeps/include/SDL.h"
//#include "SDL.h"
#include <iostream>
class Window {
private:
std::string title;
int width, height;
bool closed;
bool init();
SDL_Window* window;
SDL_Renderer* renderer;
public:
Window(const std::string&, int = 800, int = 600);
~Window();
void pollEvents();
void Clear() const;
inline bool isClosed() { return closed; }
};
#endif // !WINDOW_H
// Instantiation.cpp- just a file that includes files that I am compiling:
#include "Window.cpp"
#include "Main.cpp"
I am fairly new to doing linking and "advanced" things with makefiles. But hours of
googling I can't find out why I keep getting:
/mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:13: undefined reference to SDL_DestroyWindow' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:14: undefined reference to SDL_DestroyRenderer'
/usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:15: undefined reference to SDL_Quit' /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:19: undefined reference to SDL_Init'
/usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:23: undefined reference to SDL_CreateWindow' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:28: undefined reference to SDL_CreateRenderer'
/usr/bin/ld: Instantiation.o: in function Window::pollEvents()': /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:38: undefined reference to SDL_PollEvent'
/usr/bin/ld: Instantiation.o: in function Window::Clear() const': /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:72: undefined reference to SDL_SetRenderDrawColor'
/usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:73: undefined reference to SDL_RenderClear' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:81: undefined reference to SDL_SetRenderDrawColor'
/usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:82: undefined reference to SDL_RenderFillRect' /usr/bin/ld: /mnt/c/users/nichorsin598/source/repos/Self/TestSDL/Window.cpp:88: undefined reference to SDL_RenderPresent'
All the SDL things are undefined but I don't get an error saying SDL.h is undefined so it either has something to do with the libraries or something else
File Pathing is:
TestSDL- base directory
TestSDL\SDLDeps- includes the 2 folders include and lib
include- includes all .h files
lib- includes SDL2.dll, SDL2, SDL2Main, SDL2test
If anyone can help I'd be much obliged.
There are lots of different SDL2 libraries for different components. I use pkg-config to get the compiling and linker flags a bit like this:
SDL2_INCS := $(shell pkg-config sdl2 --cflags) \
$(shell pkg-config SDL2_image --cflags) \
$(shell pkg-config SDL2_ttf --cflags) \
$(shell pkg-config SDL2_net --cflags) \
$(shell pkg-config SDL2_mixer --cflags)
SDL2_LIBS := $(shell pkg-config sdl2 --libs) \
$(shell pkg-config SDL2_image --libs) \
$(shell pkg-config SDL2_ttf --libs) \
$(shell pkg-config SDL2_net --libs) \
$(shell pkg-config SDL2_mixer --libs)
Instantiation.o: $(OBJS).cpp
$(CC) $(COMPILER_FLAGS) $(SDL2_INCS) -o Instantiation.o $(SDL2_LIBS) $(OBJS).cpp

C++ BOOST undefined reference to `boost::filesystem::detail::copy_file

I have no clue why boost::filesystem::copy_file is making trouble for me.
undefined reference to `boost::filesystem::detail::copy_file
// g++ -std=c++11 test.cpp -lboost_filesystem -lboost_system -lrt -lboost_wave
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::create_directory("aaa");
// ok
boost::filesystem::copy_file("f1","f2");
// /tmp/ccNWZltB.o: In function `boost::filesystem::copy_file(boost::filesystem::path const&, boost::filesystem::path const&)':
// test.cpp:(.text._ZN5boost10filesystem9copy_fileERKNS0_4pathES3_[_ZN5boost10filesystem9copy_fileERKNS0_4pathES3_]+0x26): undefined reference to `boost::filesystem::detail::copy_file(boost::filesystem::path const&, boost::filesystem::path const&, boost::filesystem::copy_option, boost::system::error_code*)'
// collect2: error: ld returned 1 exit status
return 0;
}
I got no inspiration from the source code of boost or its help:
inline
void copy_file(const path& from, const path& to, // See ticket #2925
BOOST_SCOPED_ENUM(copy_option) option, system::error_code& ec)
{detail::copy_file(from, to, option, &ec);}
Even such a simple example does not work for me.
Platform: Linux Ubuntu 64
There is a workaround for this problem, replace
#include <boost/filesystem.hpp>
with
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
Or, preferably, add -DBOOST_NO_CXX11_SCOPED_ENUMS to your compiler flags
If you run into this problem make sure to include both -lboost_system and -lboost_filesystem in your call to g++
Example working Makefile
BINARY = output
FILE_OBJECTS = main.o fileLoader.o
BOOST = -lboost_system -lboost_filesystem
GCC = g++ -std=c++17
FLAGS = -Wall -pedantic -Wextra
build: $(FILE_OBJECTS)
$(GCC) $(FLAGS) $(FILE_OBJECTS) -o $(BINARY) $(BOOST)
main.o: main.cpp fileLoader.o
$(GCC) $(FLAGS) -c main.cpp
fileLoader.o: fileLoader.cpp
$(GCC) $(FLAGS) -c fileLoader.cpp
clean:
rm -rf *.o $(BINARY)
Example working code
#include <boost/filesystem.hpp>
void create_data_file(std::string file_path)
{
boost::filesystem::path p(file_path);
boost::filesystem::create_directory(p);
}
I could not compile a file that included the header boost/filesystem.hpp either. This is how I solved it: I commented out the line boost/filesystem.hpp and all the lines that were using Boost, and then compiled the file. I then uncommented all the lines in the files and compiled again, and then it worked. I was compiling with the flag -lboost_system both times!
In older boost versions it is BOOST_NO_SCOPED_ENUMS, not BOOST_NO_CXX11_SCOPED_ENUMS
see boost::filesystem::copy_file() missing symbol in c++11

scorep qt error undefined reference to `POMP2_Assign_handle'

Trying to connect simplest OpenMP QT project with score-P:
#include <QTextStream>
#include <QDateTime>
#include <QProcess>
#include <QFile>
#include <omp.h>
int main()
{
omp_set_num_threads(200);
#pragma omp parallel for
for(int i = 0; i < 200; i ++)
{
QFile file(QString("test_file_%1").arg(i));
if(file.open(QIODevice::Append))
{
QTextStream stream(&file);
stream << QDateTime::currentDateTime().toString(Qt::ISODate) + "\n";
file.close();
}
}
}
Make some changes in *.pro:
QMAKE_CXXFLAGS += -g -fopenmp
QMAKE_LIBS += -lgomp -lpthread
QMAKE_CXX = /home/monika/scorep/bin/scorep g++
QMAKE_LINK = /home/monika/scorep/bin/scorep g++
And cathing LINK error:
/home/monika/scorep/bin/scorep g++ -c -m64 -pipe -g -fopenmp -O2 -g -pipe -Wall -Wp,- D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_CORE_LIB -I/usr/lib64/qt4/mkspecs/linux-g++-64 -I../test-scorep -I/usr/include/QtCore -I/usr/include -I../../scorep/include/opari2 -I. -I../test-scorep -I. -o main.o ../test-scorep/main.cpp
/home/monika/scorep/bin/scorep g++ -m64 -Wl,-O1 -o test-scorep main.o -lgomp -lQtCore -lpthread
main.o: In function `POMP2_Init_reg_bl9o0ezmjm8_1':
/home/monika/test/build-test-scorep-Desktop-Release/main.prep.cpp.opari.inc:15: undefined reference to `POMP2_Assign_handle'
main.o: In function `main.omp_fn.0':
/home/monika/test/test-scorep/main.cpp:10: undefined reference to `pomp_tpd_'
/home/monika/test/test-scorep/main.cpp:11: undefined reference to `POMP2_Parallel_begin'
/home/monika/test/test-scorep/main.cpp:12: undefined reference to `POMP2_For_enter'
/home/monika/test/test-scorep/main.cpp:22: undefined reference to `POMP2_Implicit_barrier_enter'
/home/monika/test/test-scorep/main.cpp:24: undefined reference to `POMP2_Implicit_barrier_exit'
/home/monika/test/test-scorep/main.cpp:25: undefined reference to `POMP2_For_exit'
/home/monika/test/test-scorep/main.cpp:27: undefined reference to `POMP2_Parallel_end'
main.o: In function `main':
/home/monika/test/test-scorep/main.cpp:14: undefined reference to `POMP2_Parallel_fork'
/home/monika/test/test-scorep/main.cpp:10: undefined reference to `pomp_tpd_'
/home/monika/test/test-scorep/main.cpp:28: undefined reference to `POMP2_Parallel_join'
If i compile simple project (with only one file) like:
/home/monika/scorep/bin/scorep g++ main.cpp
Everything work (score-P too). If I change QMAKE_CXX or QMAKE_LINK to default (g++) bulding will be well, but score-P will not work.
I tried to include pomp2_lib.h (which contains functions from unfounded list), but still no result (nothing changed).

undefined reference to xercesc_2_8 when compiling sample ocde

I'm trying to compile the following sample code available at XERCES site:
#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.
XERCES_CPP_NAMESPACE_USE
int main(int argc, char* argv[])
{
try {
XMLPlatformUtils::Initialize();
}
catch (const XMLException& toCatch) {
// Do your failure processing here
return 1;
}
// Do your actual work with Xerces-C++ here.
XMLPlatformUtils::Terminate();
// Other terminations and cleanup.
return 0;
}
with,
g++ -g -Wall -pedantic -L/usr/lib -lxerces-c -o xercesTest xercesTest.cpp
giving me the following linking error:
/tmp/ccYIHCfR.o: In function `main':
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLUni::fgXercescDefaultLocale'
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_2_8::PanicHandler*, xercesc_2_8::MemoryManager*, bool)'
/home/cjmv/temp/xercesTest.cpp:18: undefined reference to `xercesc_2_8::XMLPlatformUtils::Terminate()'
/tmp/ccYIHCfR.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_2_8::XMLException'
collect2: ld returned 1 exit status
I've installed xerces-c28 and xerces-c2-dev through aptitude on my ubuntu-server 12.04
Any help would be appreciated.
Put the library last on the command line:
g++ -g -Wall -pedantic -L/usr/lib -o xercesTest xercesTest.cpp -lxerces-c
include the lib path of xerces:
try this
g++ -I/<xerces-c 2.8.0 path>/include -c xercesTest.cpp
g++ -L/<xerces-c 2.8.0 path>/lib -lxerces-c xercesTest.o

linking against clang/llvm 3.0 libraries

I've been playing around with clang/llvm 3.0, following the tutorial here
http://amnoid.de/tmp/clangtut/tut.html
The code in the tutorial is a bit outdated but after some modification, it compiles ok (both with clang++/g++, on Ubuntu):
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/Host.h>
#include <llvm/ADT/IntrusiveRefCntPtr.h>
#include <clang/Frontend/DiagnosticOptions.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/Basic/TargetOptions.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Basic/FileManager.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/Lex/HeaderSearch.h>
using namespace clang;
int main()
{
DiagnosticOptions diagOpt;
TextDiagnosticPrinter *pTextDiagPrinter = new TextDiagnosticPrinter(llvm::outs(), diagOpt);
llvm::IntrusiveRefCntPtr<DiagnosticIDs> pDiagIds(new DiagnosticIDs);
DiagnosticsEngine *pDiagEng = new DiagnosticsEngine(pDiagIds, pTextDiagPrinter);
Diagnostic diag(pDiagEng);
LangOptions lang;
FileSystemOptions fmOpt;
FileManager fm(fmOpt);
SourceManager sm(*pDiagEng, fm);
HeaderSearch headers(fm);
TargetOptions tgtOpt;
tgtOpt.Triple = llvm::sys::getHostTriple();
TargetInfo *ti = TargetInfo::CreateTargetInfo(*pDiagEng, tgtOpt);
CompilerInstance comp;
Preprocessor pp(*pDiagEng, lang, ti, sm, headers, comp);
return 0;
}
The problem is linking, I guess the problem is library order, tried various combinations, some gives thousands of undefined symbols, the best I get is with this:
g++ -fno-exceptions -fno-rtti -fno-common \
-lclangParse -lclangSerialization -lclangDriver -lclangSema -lclangAnalysis -lclangAST -lclangFrontend \
-lclangLex -lclangBasic -lclang \
`llvm-config --cxxflags --ldflags --libs` -ldl tut1.cpp
which gives
tut1.cpp:(.text.startup+0xca): undefined reference to `llvm::outs()'
tut1.cpp:(.text.startup+0xef): undefined reference to `clang::TextDiagnosticPrinter::TextDiagnostic
Printer(llvm::raw_ostream&, clang::DiagnosticOptions const&, bool)'
tut1.cpp:(.text.startup+0x104): undefined reference to `clang::DiagnosticIDs::DiagnosticIDs()'
tut1.cpp:(.text.startup+0x13d): undefined reference to `clang::DiagnosticsEngine::DiagnosticsEngine
(llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> const&, clang::DiagnosticConsumer*, bool)'
tut1.cpp:(.text.startup+0x14a): undefined reference to `clang::LangOptions::LangOptions()'
tut1.cpp:(.text.startup+0x167): undefined reference to `clang::FileManager::FileManager(clang::File
SystemOptions const&)'
tut1.cpp:(.text.startup+0x17f): undefined reference to `clang::SourceManager::SourceManager(clang::
DiagnosticsEngine&, clang::FileManager&)'
tut1.cpp:(.text.startup+0x194): undefined reference to `clang::HeaderSearch::HeaderSearch(clang::Fi
leManager&)'
tut1.cpp:(.text.startup+0x1f5): undefined reference to `llvm::sys::getHostTriple()'
tut1.cpp:(.text.startup+0x227): undefined reference to `clang::TargetInfo::CreateTargetInfo(clang::
DiagnosticsEngine&, clang::TargetOptions&)'
tut1.cpp:(.text.startup+0x232): undefined reference to `clang::CompilerInstance::CompilerInstance()
'
tut1.cpp:(.text.startup+0x277): undefined reference to `clang::Preprocessor::Preprocessor(clang::Di
agnosticsEngine&, clang::LangOptions&, clang::TargetInfo const*, clang::SourceManager&, clang::Head
erSearch&, clang::ModuleLoader&, clang::IdentifierInfoLookup*, bool, bool)'
tut1.cpp:(.text.startup+0x281): undefined reference to `clang::Preprocessor::~Preprocessor()'
tut1.cpp:(.text.startup+0x289): undefined reference to `clang::CompilerInstance::~CompilerInstance(
)'
tut1.cpp:(.text.startup+0x2e4): undefined reference to `clang::HeaderSearch::~HeaderSearch()'
tut1.cpp:(.text.startup+0x2f1): undefined reference to `clang::SourceManager::~SourceManager()'
tut1.cpp:(.text.startup+0x2fe): undefined reference to `clang::FileManager::~FileManager()'
tut1.cpp:(.text.startup+0x345): undefined reference to `clang::DiagnosticIDs::~DiagnosticIDs()'
collect2: ld returned 1 exit status
I also followed this link Linking against clang-llvm
but seems it doesn't apply to clang 3.0.
Anybody knows how to successfully build the code, or better yet, some tool like some clang-config?
just use the LLVM flags AFTER the .o file, object file won't be linked unless
the flags are written after it. A simple makefile to show what I mean
CC=clang++
LFLAGS=`llvm-config --cppflags --ldflags --libs core`
all:
$(CC) -o out in.cc $(FLAGS)
clang driver itself links with:
-lclangFrontendTool -lclangFrontend -lclangDriver -lclangSerialization -lclangCodeGen -lclangParse -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangIndex -lclangARCMigrate -lclangRewrite -lclangAST -lclangLex -lclangBasic
A little trick how to find it out: build clang, remove the clang binary, and run make -n.
Some linkers a sensitive to the order of -l* flags when working with static libraries. The of clang libraries seems OK, so try swapping `llvm-config --cxxflags --ldflags --libs` with clang libraries list.
If that don't help, only solution for you is what #SK-logic suggested - run make -n and look at compile command for clang target.