Всё, что связано с C/C++

C++ compiler / environment

Questions answered

I have got a warning dpkg-shlibdeps: warning: symbol XXX used by libfoo.so.0 found in none of the libraries.

That means that the library libfoo.so.0 uses the symbol XXX, but this symbol was not found in dependencies for this library. It looks like there is a dependency chain A → B → C, where A (libfoo) uses symbols from C. In this case introducing a new direct dependency A → C will fix the warning.

How to check that dynamic library was compiled with -fPIC

If readelf from binutils package outputs TEXTREL section, then some code was compiled without -fPIC:
$ readelf -d libtest.so | grep TEXTREL
 0x00000016 (TEXTREL)                    0x0

Run eu-findtextrel from elfutils to see the violating symbols and pipe to scanelf from pax-utils to find library containing that symbol:

$ eu-findtextrel libtest.so | sort -u | perl -ne 'while (<>) { while (/the function \x27(\w+)\x27/g) { print "$1\n"} }' | while read i; do scanelf -l -A -s $i | grep $i; done

References:

Linking with -Wl,--as-needed flags causes unresolved symbols

When compiling the following test program:
#ifdef __cplusplus
extern "C"
#endif
char pnm_readpnminit();
int main()
{
    return pnm_readpnminit();
}

the following error is produced:

# g++ -o conftest -g -Wl,--as-needed conftest_orig.cpp -lnetpbm -lm
/usr/lib64/libnetpbm.so: undefined reference to `log'
/usr/lib64/libnetpbm.so: undefined reference to `pow'
collect2: ld returned 1 exit status

  • With -Wl,--as-needed the linker will ignore all libraries, from which no symbols are used by current module. -lm was ignored and that caused the mentioned error message.
  • It also means that libnetpbm.so was incorrectly linked as it should depend on libm. However it does not.
  • If libnetpbm.so is correctly linked, -Wl,--as-needed will not break the test anymore (and one don't need to pass -lm).

See also DSO Linking.

I would like to have two target binaries for my project: dynamically linked and statically linked. How can I do it?

Currently here are two subsystems, that can automate static linking process:

pkg-config is much superior to libtool, since libtool includes all the libraries on dynamic links as well, which creates unwanted shared library dependencies and causes other problems. pkg-config has some intelligence, i.e. if you request two libs at once it will nuke duplicate flags and order the -l flags properly. Also, it can gripe about missing dependencies or conflicting libraries. Note that pkg-config merges at query-time: if gtk+.pc file contains CFLAGS that are specific to that library, pkg-config –cflags gtk+ also outputs CFLAGS for all dependencies of gtk+.

See also:

When I install MinGW I got the error(s) Get package: XXX download failed

Installation reports errors like following:
C:\MinGW\bin\mingw-get.exe: *** ERROR *** Get package: http://prdownloads.sourceforge.net/mingw/openssl-1.0.0-1-msys-1.0.13-bin.tar.lzma?download: download failed
C:\MinGW\bin\mingw-get.exe: *** ERROR *** Get package: http://prdownloads.sourceforge.net/mingw/libiconv-1.13.1-1-mingw32-dll-2.tar.lzma?download: download failed

Try to re-run the installation for failed packages again:
mingw-get.exe install libiconv
mingw-get.exe install msys-openssl

What compiler option needs to be checked for CygWin / MinGW compiler?

For CygWin use CYGWIN and and mingw use MINGW32.

So the conditional check for any other Win32 compiler will be:

#if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__MINGW32__)
#include <windows.h>
#endif

Cygwin/MinGW linker adds ordinals after the DLL export symbols. How to remove them, as I get java.lang.UnsatisfiedLinkError?

$ g++ -shared cpp_wrap.o -o libmy_java.so
$ nm libcrfpp_java.so | grep CRFPPJNI_VERSION
64303a43 T _Java_org_chasen_crfpp_CRFPPJNI_VERSION_1get@8

What should I do to remove @n (e.g. get _Java_org_chasen_crfpp_CRFPPJNI_VERSION_1get)?

From here:
Calling Convention Internal MSVC DLL (w/ DEF) MSVC DLL (dllexport) DMC DLL MinGW DLL BCC DLL
__stdcall _function@n function _function@n _function@n function@n function
__cdecl _function function function function function _function

so you need to use cdecl instead of stdcall in function declaration:

#ifdef __MINGW32__
#define JNICALL __cdecl
#endif

On the other hand, the options -Wl,–add-stdcall-alias and -Wl,–kill-at should work just fine, but they don't in some cases.

jni_md.h refers the __int64 type, which is unknwon for GCC compiler

Use the workaround use the following:
#if defined(__GNUC__) && !defined(__INTEL_COMPILER)
typedef long long __int64;
#endif
 
#include <jni.h>

or use gcc -D__int64="long long" ... (check Generating .dll files for JNI under Windows/Cygwin/GCC and Building w32 JNI DLLs with gcc)

How to dump all preprocessor definitions?

Use g++ -dD -E -I... source.cpp.

How to define verbosity in Makefile (control the messages to be displayed) ?

From Controlling verbosity of make:

Makefile

V = 0
CC_0 := @echo -t "Compiling $<..."; $(CC)
CC_1 := $(CC)
CC = $(CC_$(V))
 
%.o: %.c
    $(CC) $(CFLAGS) -c $< -o $@

Afterwards make V=0 … will do the job.

How to enable STL support for gdb print command?

From here:
  • Checkout Python scripts to ~/.gdb/libstdcxx_printers: cd ~/.gdb && svn co http://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python libstdcxx_printers
  • Add to your ~/.gdbinit:
    python
    import sys, os
    sys.path.insert(0, os.path.expanduser('~/.gdb/libstdcxx_printers'))
    from libstdcxx.v6.printers import register_libstdcxx_printers
    register_libstdcxx_printers(None)
    end

, , ,
programming/cpp.txt · Last modified: 2010/11/23 17:54 by dmitry
 
 
Recent changes RSS feed Driven by DokuWiki