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.
Related
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
While compiling/linking this code:
#include <string>
using std::string;
#include <pcrecpp.h>
using pcrecpp::RE;
int main() {
string
subj ("Hello world!"),
rgx ("lolCat([0-9])"),
result;
RE(rgx).FullMatch(subj, &result);
}
... by using this command:
i586-mingw32msvc-g++ -std=c++11 -o test.exe -Ipcre-install/include test.cpp \
pcre-install/lib/libpcre.a \
pcre-install/lib/libpcrecpp.a \
pcre-install/lib/libpcreposix.a
... I get this error(s):
/tmp/ccAR72nT.o:test.cpp:(.text+0x119): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x123): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x12d): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x137): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x141): undefined reference to `_imp___ZN7pcrecpp2RE6no_argE'
/tmp/ccAR72nT.o:test.cpp:(.text+0x14b): more undefined references to `_imp___ZN7pcrecpp2RE6no_argE' follow
/tmp/ccAR72nT.o:test.cpp:(.text+0x1bd): undefined reference to `_imp___ZNK7pcrecpp2RE9FullMatchERKNS_11StringPieceERKNS_3ArgES6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_S6_'
/tmp/ccAR72nT.o:test.cpp:(.text+0x1d6): undefined reference to `_imp___ZN7pcrecpp2RED1Ev'
/tmp/ccAR72nT.o:test.cpp:(.text+0x294): undefined reference to `_imp___ZN7pcrecpp2RED1Ev'
/tmp/ccAR72nT.o:test.cpp:(.text$_ZN7pcrecpp3ArgC1EPSs[__ZN7pcrecpp3ArgC1EPSs]+0x16): undefined reference to `_imp___ZN7pcrecpp3Arg12parse_stringEPKciPv'
/tmp/ccAR72nT.o:test.cpp:(.text$_ZN7pcrecpp2REC1ERKSs[__ZN7pcrecpp2REC1ERKSs]+0x6a): undefined reference to `_imp___ZN7pcrecpp2RE4InitERKSsPKNS_10RE_OptionsE'
collect2: error: ld returned 1 exit status
Am I doing anything wrong?
libpcrecpp was compiled with this configuration:
../pcre3-8.35/configure --host=i586-mingw32msvc \
--enable-shared=no --enable-static=yes \
--prefix="$(readlink -m ../pcre-install)"
You have compile pcrecpp as static library and you need to define PCRE_STATIC when compiling your code, see https://github.com/vmg/pcre/blob/a257f5c7acc12e64dc2b5aa170b8e4b87dc34f83/pcreposix.h#L117
i586-mingw32msvc-g++ -std=c++11 -o test.exe -DPCRE_STATIC -Ipcre-install/include test.cpp \
pcre-install/lib/libpcre.a \
pcre-install/lib/libpcrecpp.a \
pcre-install/lib/libpcreposix.a
Without PCRE_STATIC all public functions marked as dllimport and have different name mangling
You must define PCRE_STATIC (i.e. -DPCRE_STATIC).
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).
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.
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