LLVM Error : External function could not be resolved - c++

I am reading the LLVM's Kaleidoscope tutorial (http://llvm.org/docs/tutorial/index.html).
I wanted to compile and test the language. After some compiler's errors (EngineBuilder and Module's constructor, linking libs...), the example program was built. Then, I tried the language. I got a few problems with InitializeNativeTargets, DataLayoutPass... But I managed to correct them.
Howewer, I don't manage to resolve one error. When I write extern printd(x); printd(5);, the program doesn't work : "LLVM ERROR : Program used external function 'printd' which could not be resolved".
I looked for the solution on the net. I read a lot of webpages, but nothing worked.
How can I resolve this problem ? Why LLVM can't find the external function 'printd', which is included in the program ? Thanks in advance for your answer.
The used code : https://docs.google.com/document/d/1Qb-zUGaUUIF354uFCXv1iuq8n_rjya6IHDW4WCPWN_4/edit?usp=sharing
The .pro file (Qt Creator) :
QT += core
QT -= gui
TARGET = Kaleidoscope
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
DEPENDPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
LIBS += `llvm-config --ldflags --libs all --system-libs` -Wl,-no-as-needed
QMAKE_CFLAGS += -m32
QMAKE_CXXFLAGS += -rdynamic -std=c++11 -O3
I use GCC 4.8.2, LLVM 3.5 and Qt 5.3.1 on Ubuntu 14.04 32bits.

Finally, I found a great link on the net : http://koichitamura.blogspot.fr/2011/01/since-i-went-to-held-several-weeks-ago.html.
The error came from the wrong place of the -rdynamic argument in the .pro file (-rdynamic must be after the linking options). You need this argument because (GCC man page) :
This instructs the linker to add all symbols, not only used ones, to the dynamic symbol table.
I changed the .pro file :
QT += core
QT -= gui
TARGET = Test01
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
DEPENDPATH += /home/alainetflo/Documents/me/C++/LLVM/llvm-3.5.0.src/include
INCLUDEPATH += /usr/include/i386-linux-gnu/c++/4.8 \
/usr/include/c++/4.8
LIBS += `llvm-config --ldflags --libs all --system-libs` -Wl,-no-as-needed -rdynamic
QMAKE_CFLAGS += -m32
QMAKE_CXXFLAGS += -g -std=c++11 -O3
In this way, the program works with no error ! (The tutorial's example runs (http://llvm.org/docs/tutorial/LangImpl6.html#kicking-the-tires))

Related

Errors while using Libtorch + OpenCV + QT Creator

I have the following configuration in the .pro file
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
CONFIG += thread
SOURCES += main.cpp
INCLUDEPATH += /usr/local/include/opencv4
LIBS += -L/usr/local/lib/
LIBS += -lopencv_core
LIBS += -lopencv_highgui
LIBS += -lopencv_imgproc
LIBS += -lopencv_videoio
QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0
INCLUDEPATH += /path/to/libtorch/include
INCLUDEPATH += /path/to/libtorch/include/torch/csrc/api/include
LIBS += -L/path/to/libtorch/lib
LIBS += -ltorch -lc10
OpenCV works absolutely fine without "QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0". With this, however, I get this following errors:
OpenCV works fine with "QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=1" as well. But it throws a different set of errors:
Setting "QMAKE_CXXFLAGS += -D_GLIBCXX_USE_CXX11_ABI=0" has been recommended for Libtorch in most of the forums to avoid the errors above.
What could be a solution or some solutions to work around this?
(I am a newbie to both Libtorch and Qt Creator.)
Maybe OpenCV and Libtorch were compiled with a different version of GCC (and different values of _GLIBCXX_USE_CXX11_ABI).
Try recompiling them by yourself and see if things change.
The problem is that you downloaded the wrong ABI version of LibTorch. It looks like that you downloaded the Pre-CXX11 ABI version of LibTorch and OpenCV is compiled with CXX11 ABI. So if you set _GLIBCXX_USE_CXX11_ABI=0, OpenCV throws errors, and if you set _GLIBCXX_USE_CXX11_ABI=1, LibTorch throws errors.
Download the CXX11 ABI LibTorch from PyTorch official website and you won't need to set the _GLIBCXX_USE_CXX11_ABI flag.
About the dual ABI problem of GCC, see https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

Link Boost with Qt Creator on Mac

I have installed boost using Homebrew and got everything set up in my .pro file.
myProFile
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
src/nmea-tests.cpp \
src/position.cpp \
src/utilities.cpp \
src/parsenmea.cpp
HEADERS += \
headers/parseNMEA.h \
headers/position.h \
headers/types.h \
headers/utilities.h
INCLUDEPATH += headers/
LIBS += -lboost_unit_test_framework
macx {
QMAKE_CFLAGS += -std=c++11 -stdlib=libc++
QMAKE_CXXFLAGS += -std=c++11 -stdlib=libc++
LIBS += -L"/usr/local/Cellar/boost/1.63.0/lib" -lboost_random
INCLUDEPATH += "/usr/local/Cellar/boost/1.63.0/include"
}
However, when I try to build the project, Qt can't find boost/test/unit_test.hpp file
Compiler Error Message:
/Users/hadyfarhat/Documents/ntu/courses/soft/task4/gps/src/nmea-tests.cpp:3: error: 'boost/test/unit_test.hpp' file not found
#include <boost/test/unit_test.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~~
I would say the better way is using /usr/local/opt/boost for Intel and /opt/homebrew/opt/boost for Apple Silicon in your case. Then you don't need to worry about the problem of the version.
I had to change boost version from 1.63.0 to 1.66.0.
For Future problems with boost use my question as a guide and be sure to install homebrew before that.

Undefined reference to symbol 'gzclose'

I guess this is a linker error, but I've spent a lot of time and haven't find a working answer. I have an OpenCV C++ program, when I try to compile something of the style cv::Mat newMatObject; it gives me the error /usr/local/lib/libopencv_core.a(persistence.cpp.o): undefined reference to symbol 'gzclose'.
The .pro file contains the following lines:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = QtTest
TEMPLATE = app
INCLUDEPATH += /usr/local/include/opencv
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lz -ltbb -ltiff
SOURCES += main.cpp\
qttest.cpp
HEADERS += qttest.h
FORMS += qttest.ui
Am I forgetting something/doing something wrong?
Some Info:
Kubuntu 16.04 LTS 64 Bits
Qt Creator 4.0.2, based on Qt 5.7.0
OpenCV 2.4.13
Addendum:
I had the program working properly with OpenCV 2.4.9, no other changes. I decided to move on to 2.4.13 because of the extended gui, which didn't work on 2.4.9. I downloaded OpenCV 2.4.13 from the official website, and installed it with this script (except for the wget and unzip, which were made manually), initially didn't autoremove the previous library, but I did after the issue came up and rerun the previous script again, to ensure proper installation.
Edit 1:
Running
g++ -o opencvtest opencvtest.cpp `pkg-config opencv --cflags --libs`
with similar lines of code provides the desired result without a problem.
Add -lz to your LIBS:
LIBS += -L/usr/local/lib -lopencv_core -lopencv_highgui -lz

Qt project set to version 5 but qtcreator still looking for qt4 header files?

Here is a screenshot from the project view:
Here is the .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2014-05-18T15:34:14
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = bedcoll
CONFIG += console
CONFIG -= app_bundle
QMAKE_CC = clang
QMAKE_CXX = clang++
QMAKE_CXXFLAGS += -std=c++11
QMAKE_CXXFLAGS += -D_FILE_OFFSET_BITS=64
INCLUDEPATH += /usr/include/c++/4.8
INCLUDEPATH += /usr/include/boost
LIBS += -lboost_system
LIBS += -lboost_filesystem
LIBS += -lboost_program_options
TEMPLATE = app
SOURCES += \
src/util.cpp \
src/bedcoll.cpp \
src/main.cpp
HEADERS += \
src/collgen_memo.h \
src/util.h \
src/bedcoll.h \
src/error_enum.h
This project builds without any problem in a kubuntu 13.10, but after upgrading to 14.04, it gives the following error:
/usr/include/qt4/QtCore/qstring.h:1045: error: undefined reference to `QString::toAscii() const'
/usr/include/qt4/QtCore/qstring.h:880: error: undefined reference to `QString::free(QString::Data*)'
/usr/include/qt4/QtCore/qstring.h:879: error: undefined reference to `QString::shared_null'
Kubuntu 14.04 ships qt5 as default, but qtcreator is still looking for qt4, very confusing.
#Netjat is right, cleaning up solves the problem, in fact there is a shortcut, just right click on the project and choose "rebuild".

Adding libusb library to a Qt project in osx

I've been attempting for the past 16 hours to attach the libusb library to a Qt project without much success. I would appreciate any input on the matter, it's getting frustrating.
The .pro file is this:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>
Source code:
#include <iostream>
#include <libusb.h>
using namespace std;
int main()
{
cout << "Hello World!" << endl;
return 0;
}
Compiler output:
13:01:50: Running steps for project lallala...
13:01:50: Configuration unchanged, skipping qmake step.
13:01:50: Starting: "/usr/bin/make" -w
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
/Users/MAXIMUS/Qt5.0.0/5.0.0/clang_64/bin/qmake -spec macx-g++42 CONFIG+=debug CONFIG+=x86_64 CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../lallala/lallala.pro
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
make: Entering directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o -L/usr/local/lib -libusb-1.0.a -L<libusb.h>
/bin/sh: -c: line 0: syntax error near unexpected token `newline'
/bin/sh: -c: line 0: `g++-4.2 -headerpad_max_install_names -mmacosx-version-min=10.6 -o lallala main.o -L/usr/local/lib -libusb-1.0.a -L<libusb.h> '
make: *** [lallala] Error 2
make: Leaving directory `/Users/MAXIMUS/Documents/workspace/lallala-build-Desktop_Qt_5_0_0_clang_64bit_SDK-Debug'
13:01:50: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project lallala (kit: Desktop Qt 5.0.0 clang 64bit (SDK))
When executing step 'Make'
Forming my comment into a proper answer; this is not the correct syntax to use:
LIBS += -L/usr/local/lib -libusb-1.0.a
LIBS += -L<libusb.h>
The proper one would be this:
LIBS += -L/usr/local/lib -lusb-1.0
or
LIBS += -l/full/path/to/libusb-1.0.a
You can drop the second LIBS line in your initial attempt because you have already specified the path in the former, and putting an "include" statement in there would not be reasonable anyhow. So, this is what you could write for your complete .pro file:
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
INCLUDEPATH +=/usr/local/include/libusb-1.0
LIBS += -L/usr/local/lib -lusb-1.0
This is not Qt specific, just generic linkage issue: -lfoo extends to $(prefix)foo$(suffix), where the prefix and suffix are figured out automatically based on the platform. That is, the prefix would be lib in your case, and suffix would be either .a or .so on Unix, probably .dylib on Mac, etc.
You may wish to look into pkg-config support if it is possible to establish. In that case, you would write something like this what we did in QtSerialPort:
CONFIG += link_pkgconfig
PKGCONFIG += libudev
Yet another option is to add the GUI through the QtCreator IDE or similar IDE that you may be using. There is an option usually in the "Linker" section to add a library. Here are two screenshots from my QtCreator:
Click on the project name on the left in the project source tree navigator, and select Add Library. Then the first screenshot will come up, and you can select the external option, and then you can see the second.
It is needless to say that you would need to run qmake after these changes to generate the corresponding Makefile on your desired platform.
Syntax is the following:
-L%LIBRARY_PATH% to make a specific path visible and
-l%LIBRARY_NAME% to link a specific library that is located in a visible path
so I guess this should work (I don't think you need the .a extension):
LIBS += -L/usr/local/lib -llibusb-1.0
and I have no idea what this would do:
LIBS += -L
so I guess I'd remove it.
Once fixed run qmake then build...
Hope it helps...