“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

 

GCC Build With Strange Errors

link: http://modest-destiny.net/wp/?p=77

GCC Build With Strange Errors

I have been fighting with these strange errors for some days, and finally I got it.

Problem

The core problem is that my C_INCLUDE_PATH and CPLUS_INCLUDE_PATHends with a colon. Path ending with a colon will include current path, aka./ which caused the error.

Solution

We need to remove the trailing colon, so I write a bash function for it.

# usage: removeTrailingColon str
# example:
#    C_INCLUDE_PATH=$(removeTrailingColon $C_INCLUDE_PATH)
removeTrailingColon() {
    if [[ $1 == *: ]]
    then
        local strColonRemoved=$1;
        local strColonRemoved=${strColonRemoved:0:(-1)};
        echo $strColonRemoved;
    else
        echo $1
    fi
}

Usage is like this:

export C_INCLUDE_PATH=$(removeTrailingColon $C_INCLUDE_PATH)
export CPLUS_INCLUDE_PATH=$(removeTrailingColon $CPLUS_INCLUDE_PATH)
export LD_LIBRARY_PATH=$(removeTrailingColon $LD_LIBRARY_PATH)
export LIBRARY_PATH=$(removeTrailingColon $LIBRARY_PATH)

Elaborate on problem

I’ll list the error I encountered building gcc-4.7.3, so that people will be able to access it through a search engine:

In file included from ../.././gcc/c-lang.c:24:
../.././gcc/system.h:499: error: conflicting types for strsignal
/usr/include/string.h:566: note: previous declaration of strsignal was here
In file included from ./tm.h:19,
                 from ../.././gcc/c-lang.c:26:
./options.h:3750:2: error: #error too many masks for ix86_isa_flags
In file included from ../.././gcc/input.h:25,
                 from ../.././gcc/tree.h:27,
                 from ../.././gcc/c-lang.c:27:
../.././gcc/../libcpp/include/line-map.h:208: error: CHAR_BIT undeclared here (not in a function)
../.././gcc/../libcpp/include/line-map.h:208: error: bit-field reason width not an integer constant
../.././gcc/../libcpp/include/line-map.h:208: warning: reason is narrower than values of its type
In file included from ../.././gcc/tree.h:32,
                 from ../.././gcc/c-lang.c:27:
../.././gcc/real.h:87:5: error: division by zero in #if
../.././gcc/real.h:87:5: error: division by zero in #if
../.././gcc/real.h:90:6: error: division by zero in #if
../.././gcc/real.h:90:6: error: division by zero in #if
../.././gcc/real.h:93:7: error: division by zero in #if
../.././gcc/real.h:93:7: error: division by zero in #if
../.././gcc/real.h:96:8: error: division by zero in #if
../.././gcc/real.h:96:8: error: division by zero in #if
../.././gcc/real.h:99:9: error: division by zero in #if
../.././gcc/real.h:99:9: error: division by zero in #if
../.././gcc/real.h:102:10: error: division by zero in #if
../.././gcc/real.h:102:10: error: division by zero in #if
../.././gcc/real.h:105:9: error: #error "REAL_WIDTH > 6 not supported"
In file included from ../.././gcc/c-family/c-common.h:26,
                 from ../.././gcc/c-tree.h:25,
                 from ../.././gcc/c-lang.c:28:
../.././gcc/../libcpp/include/cpplib.h:225: error: bit-field type width not an integer constant
../.././gcc/../libcpp/include/cpplib.h:225: warning: type is narrower than values of its type
In file included from ../.././gcc/c-family/c-common.h:26,
                 from ../.././gcc/c-tree.h:25,
                 from ../.././gcc/c-lang.c:28:
../.././gcc/../libcpp/include/cpplib.h:267:3: error: #error "Cannot find a least-32-bit signed integer type"
../.././gcc/../libcpp/include/cpplib.h:269: error: expected ‘=’, ‘,’, ‘;’, asm or __attribute__ before cppchar_t
../.././gcc/../libcpp/include/cpplib.h:270: error: expected ‘=’, ‘,’, ‘;’, asm or __attribute__ before cppchar_signed_t
../.././gcc/../libcpp/include/cpplib.h:768: error: expected ‘=’, ‘,’, ‘;’, asm or __attribute__ before cpp_interpret_charconst
../.././gcc/../libcpp/include/cpplib.h:779: error: expected ‘=’, ‘,’, ‘;’, asm or __attribute__ before cpp_host_to_exec_charset
../.././gcc/../libcpp/include/cpplib.h:954: error: expected ‘=’, ‘,’, ‘;’, asm or __attribute__ before cpp_parse_escape
make[2]: *** [c-lang.o] Error 1

QuartusII安裝出現libXext.so.6: cannot open shared object file錯誤的解決方案

Ubuntu 64-bit在安裝QuartusII的時候可能會出現:

libXext.so.6: cannot open shared object file

這樣的錯誤。

你可能locate libXext.so.6也能找到它,但其實QuartusII所要import的是32位的libXext.so.6。所以,簡單的辦法就是將32位的運行庫一起裝在機子上就可以了。

解決:

sudo apt-get install ia32-libs

很可能你的其他的安裝遇到“libXext.so.6和libXtst.so.6無法找到、無法打開”等問題也能使用這種方案解決。

 

[轉載] Linux下递归列出仅所有常规文件或文件夹路径的方法

 

看到他說的第一句話,非常感同身受啊。。。於是就轉載過來了。

感謝:http://jakwings.is-programmer.com/posts/35628.html

 

混蛋的,好不容易才知道,立刻发篇博文说一下……囧

 

## 只列出常规文件
find ./ -type f
 
## 只列出文件夹
find ./ -type d

find的man page介绍太长了,眼花缭乱(用zsh的可无视= =;)!百度和GOOGLE的结果也太烂了,几乎全是ls方法。

 

既然用到find了,就不得不提它的另一个功能了:

 

## 对找到的所有文件进行批处理
 
find . -type f -exec chmod 644 {} \; # 后面的\;必须的,表示按行输出
 
find . -type d -exec chmod 755 {} \; # {} 表示找到的文件路径

 

好了,暂时介绍完毕,大家还是继续man find去吧……

 

自定义bash提示符 -- Bash prompt basics

Bash中内置了PS1/PS2/PS3/PS4及PROMPT_COMMAND共5个变量,用于控制Bash shell中提示符的内容和格式。
例如一般意义上默认的:user@localhost~ $,本文的意图是让我们能控制、更改它。
 
如果你们需要在全局设置这些选项,请前往:
(全局的系统设置文件:)/etc/profile, /etc/bashrc
(用户的系统设置文件:)~/.bash_profile , ~/.bash_login , ~/.profile , ~/.bashrc, ~/.bash_logout

Read more