“Undefined reference to …” How can I find the library to link to?

“Undefined reference to …” How can I find the library to link to?

The most panicking error among all the make errors is the undefined reference error, which is mostly cause by an unlinked library.

For instance, here I forget to link to OpenGL glfw on purpose, so gcc reports the error as following:

main.o: In function `Update':
main.cpp:375: undefined reference to `glfwGetCursorPos'
main.cpp:377: undefined reference to `glfwSetCursorPos'
main.o: In function `AppMain()':
main.cpp:400: undefined reference to `glfwSetErrorCallback'
main.cpp:405: undefined reference to `glfwWindowHint'
main.cpp:406: undefined reference to `glfwWindowHint'
main.cpp:407: undefined reference to `glfwWindowHint'
main.cpp:408: undefined reference to `glfwWindowHint'
main.cpp:409: undefined reference to `glfwWindowHint'
main.cpp:410: undefined reference to `glfwCreateWindow'
main.cpp:415: undefined reference to `glfwSetInputMode'
main.cpp:416: undefined reference to `glfwSetCursorPos'
main.cpp:417: undefined reference to `glfwSetScrollCallback'
main.cpp:418: undefined reference to `glfwMakeContextCurrent'
main.cpp:494: undefined reference to `glfwSetWindowShouldClose'
main.cpp:475: undefined reference to `glfwWindowShouldClose'
error: ld returned 1 exit status

Most of the time, these errors are not bugs in our code, they’re just unlinked libraries. We call a function in our code, however the compiler cannot determine the next instruction under that it cannot find the real part(entry) of the function.

Therefore, I wrote a simple shell script that might help us find the undefined reference, the unknown library.

Here’s the script:

function findundef() {
    [ -z "$1" ] || [ -z "$2" ] && {
        echo "usage: findundef 'library_dir' 'undefined_symbol'"
        echo "'library_dir' can be separated by ':'"
        return 1
    }

    local libraryDir
    libraryDir=($(echo "$1" | tr ":" " "))

    for i in "${libraryDir[@]}"; do
        find "$i" -type f -regextype posix-egrep -iregex '(.*\.)((a)|(so))' \
          -exec nm -gAC --defined-only {} 2>>/tmp/findundef.err \; \
            | grep "$2"
    done

    [ "$?" -ne 0 ] && cat /tmp/findundef.err

    rm -f /tmp/findundef.err
    unset libraryDir
}

Let’s first take our example as a tutorial and let’s find the function glfwGetCursorPos

$ findundef "/usr/lib/:/usr/local/lib/" "glfwGetCursorPos"

Here’s the output of the run:

Picture1

 

Using CMake and C++11 With Eclipse

Eclipse is a Good IDE for C++.

And, CMake is a useful Makefile generation tool.

We hope that we might integrate them together.

1 Quick Start with Old Projects

mkdir buildeclipse
cd buildeclipse/
cmake ../SOURCE_FILES/ -G"Eclipse CDT4 - Unix Makefiles"\
    -DCMAKE_ECLIPSE_VERSION=4.3\
    -DCMAKE_CXX_COMPILER_ARG1=-std=c++11

Note:

If you get an error of like this:

Could not determine Eclipse version, assuming at least 3.6 (Helios). Adjust CMAKE_ECLIPSE_VERSION if this is wrong.

use -D_ECLIPSE_VERSION=4.3 instead of -DCMAKE_ECLIPSE_VERSION=4.3.

2 A Project Example

PROJECT(HELLO)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

# Optional
# or SET(_ECLIPSE_VERSION 4.3). If you add these, you may omit `-DCMAKE_ECLIPSE_VERSION=4.3`
SET(CMAKE_ECLIPSE_VERSION 4.3)

ADD_EXECUTABLE(main main.cpp)
TARGET_LINK_LIBRARIES(main pthread)

Refs:

CDT/User/FAQ

CMake:Eclipse UNIX Tutorial

stackoverflow

Let OMNet++(OMNetpp) support c++11 or c++0x standard

There're a lot of people asking how to customly define c++ compile flags (CXX_FLAGS) under OMNet++ 4 (OMNetpp 4), but don't really get an answer.

Here is the answer now. First follow my instruction and then I'll explain the reason.

Important: These are instructions under Linux. However, it's still very easy to port these instructions to Windows. Try it yourself.

  • mkdir -p ~/.omnetpp/
  • cp $omnetpp_root/Makefile.inc ~/.omnetpp/
    • if you do not know about the omnetpp_root environment variable, you probably have a false setup of OMNet++.
  • vim ~/.omnetpp/Makefile.inc
  • add -std=c++11 to CFLAGS_DEBUG and CFLAGS_RELEASE
  • You may edit the Makefile.inc as you wish as long as you know what you're doing.
  • vim ~/.bashrc
  • Append export OMNETPP_CONFIGFILE="~/.omnetpp/Makefile.inc" to the end of your ~/.bashrc file
  • source ~/.bashrc
  • Finally, rebuild your project either with Eclipse or opp_makemake.
  • Enjoy!

Reasons Here:

If you examine the Makefile generated by opp_makemake carefully, something may catch your eyes.

# Pull in OMNeT++ configuration (Makefile.inc or configuser.vc)

ifneq ("$(OMNETPP_CONFIGFILE)","")
CONFIGFILE = $(OMNETPP_CONFIGFILE)
else
ifneq ("$(OMNETPP_ROOT)","")
CONFIGFILE = $(OMNETPP_ROOT)/Makefile.inc
else
CONFIGFILE = $(shell opp_configfilepath)
endif
endif

This part of Makefile code pull in the configuration defined by the users (You!).

Where to pull?

The environment variable: OMNETPP_CONFIGFILE

so ... set it and enjoy whatever you like, either c++11 or openmp or valgrind, etc.

 

link: http://blog.modest-destiny.net/omnetpp/2014/01/05/Let-OMNet++(OMNetpp)-support-c++11-or-c++0x-standard/

Qt 5.1.0 on Ubuntu Linux, MySQL database connection problem, driver not loaded problem

Qt 5.1.0 on Ubuntu Linux, MySQL database connection problem, driver not loaded problem


Problem description

QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QPSQL QPSQL7
false

Read more

Ubuntu 12.04 Install Gazebo ROS Package (gazebo_ros_pkgs) 1.9 From source

Ubuntu 12.04 Install Gazebo ROS Package (gazebo_ros_pkgs) 1.9 From source

Read more