“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

Add Apache2 Perl Support in Ubuntu 12.04

Add Apache2 Perl Support in Ubuntu 12.04

  • sudo apt-get install libapache2-mod-perl2 (if Ubuntu Server use aptitude install)
  • ( if the perl-mod is not enabled, you may use: sudo a2enmod perl )
  • mkdir ~/www/cgi-bin (it could be anywhere, just take it as an example)
  • Add the following code to /etc/apache2/sites-enabled/000-default file before </VirtualHost> line.
# Use 'ModPerl' module to process '.pl' and '.cgi' file
<Files ~ "\.(pl|cgi)$">
    SetHandler perl-script
    PerlResponseHandler ModPerl::PerlRun
    Options +ExecCGI
    PerlSendHeader On
</Files>

# Use '/home/www/cgi-bin/' as a CGI Directory
ScriptAlias /cgi-bin/ /home/www/cgi-bin/
<Directory "/home/www/cgi-bin/">
    Options ExecCGI
    AddHandler cgi-script cgi pl
</Directory>
  • Restart the apache2 service: sudo service apache2 restart
  • Test the perl environment:
  • Create file test.pl in /home/www/cgi-bin/:
#!/usr/bin/perl -w
print "Content-type: text/html\r\n\r\n";
print "Hello there!<br />\nJust testing .<br />\n";

for ($i=0; $i<10; $i++)
{
print $i."<br />";
}
  • Note 1: #!/usr/bin/perl -w is used to locate the perl parser which could be located by which perl.
  • Note 2: code print "Content-type: text/html\r\n\r\n"; is required to make it a valid CGI program.

chkconfig on Ubuntu 12.04 causing '/sbin/insserv: No such file or directory' Problem

chkconfig on Ubuntu 12.04 causing ‘/sbin/insserv: No such file or directory’ Problem

Problem Description:

I tried to start sshd at boot using chkconfig on Ubuntu 12.04.

As a result, I used `chkconfig --level 35 ssh on` to start it as default, however I got the following error.

/sbin/insserv: No such file or directory
/sbin/insserv: No such file or directory

How to solve:

ln -s /usr/lib/insserv/insserv /sbin/insserv

 

Emacs (line break/buffer coding system) Conversion From DOS to UNIX and Vice Versa

If in emacs you need a different file coding system (line terminator/line break), for example you are on a windows system and need to type a unix like text file (or vice versa), you can easily convert the buffer coding system.

DOS/Windows to UNIX


M-x set-buffer-file-coding-system RET undecided-unix

or


C-x RET f undecided-unix
C-x C-f

UNIX to DOS/Windows


M-x set-buffer-file-coding-system RET undecided-dos

or


C-x RET f undecided-dos
C-x C-f

Ref: http://edivad.wordpress.com/2007/04/03/emacs-convert-dos-to-unix-and-vice-versa/