Cmake make exe gnutools - animeshtrivedi/notes GitHub Wiki

Careful!

With CMake, you have to be careful with setting up 'CMAKE_C_STANDARD' vs 'CMAKE_CXX_STANDARD' flags with all other variable. All files with a .c extension will match to _C_ standards, and all .cpp or .cxx file will go to _CXX_ macros.

  • CMAKE_C_STANDARD
  • CMAKE_C_STANDARD_REQUIRED
  • CMAKE_C_EXTENSIONS
  • CMAKE_C_COMPILER
  • CMAKE_C_FLAGS

errors on code

https://stackoverflow.com/questions/53217370/undefined-reference-to-le16toh-error-in-makefile

undefined reference to `le32toh'

Needs _DEFAULT_SOURCE

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb -Wl,--no-as-needed -Wall -Wextra -Wundef -D_POSIX_C_SOURCE=200809L -D_DEFAULT_SOURCE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -fno-omit-frame-pointer")

Cmake Prefix

https://stackoverflow.com/questions/6003374/what-is-cmake-equivalent-of-configure-prefix-dir-make-all-install

cmake -DCMAKE_INSTALL_PREFIX=/usr . 

gcc nostdlib

-nostdlib is a superset of -nodefaultlibs and -nostartfiles . It also disables virtual table verification. So -nostdlib is sufficient to disable all the related features; -nodefaultlibs and -nostartfiles don't add anything to it. (But it doesn't hurt to mention them too.)

https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Link-Options.html (new: https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html)

  • -nostartfiles : Do not use the standard system startup files when linking. The standard system libraries are used normally, unless -nostdlib or -nodefaultlibs is used.

  • -nodefaultlibs : Do not use the standard system libraries when linking. Only the libraries you specify will be passed to the linker. The standard startup files are used normally, unless -nostartfiles is used. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified.

  • -nostdlib : Do not use the standard system startup files or libraries when linking. No startup files and only the libraries you specify will be passed to the linker. The compiler may generate calls to memcmp, memset, memcpy and memmove. These entries are usually resolved by entries in libc. These entry points should be supplied through some other mechanism when this option is specified. One of the standard libraries bypassed by -nostdlib and -nodefaultlibs is libgcc.a, a library of internal subroutines that GCC uses to overcome shortcomings of particular machines, or special needs for some languages. (See Interfacing to GCC Output, for more discussion of libgcc.a.) In most cases, you need libgcc.a even when you want to avoid other standard libraries. In other words, when you specify -nostdlib or -nodefaultlibs you should usually specify -lgcc as well. This ensures that you have no unresolved references to internal GCC library subroutines. (For example, `__main', used to ensure C++ constructors will be called; see collect2.)

g++ undefined reference to typeinfo

g++ undefined reference to typeinfo

This can also happen when you mix -fno-rtti and -frtti code. Then you need to ensure that any class, which type_info is accessed in the -frtti code, have their key method compiled with -frtti. Such access can happen when you create an object of the class, use dynamic_cast etc.

How to find a shared library compiled

https://stackoverflow.com/questions/22150806/how-can-i-check-if-a-library-was-compiled-with-fno-rtti

If a class has virtual. functions, it should have type info. Do nm -C libname.so and watch for "vtable for", "typeinfo for", and "typeinfo name for".

pkg-config

What is the proper way to use pkg-config from cmake?

How to give search path to configure

CPPFLAGS=-I$HOME/local/include
LDFLAGS=-L$HOME/local/lib
then call 
./configure 

useful in fio when building libzbc in a non-standard path. https://stackoverflow.com/questions/7561509/how-to-add-include-and-lib-paths-to-configure-make-cycle

compile and make executable but do not install them

https://stackoverflow.com/questions/13381742/cmake-exclude-executable-during-install

install targets specifically needs to be identified

  • anp cmake only install the shared library, but not the anp_server and anp_client program

library order matters

https://stackoverflow.com/questions/30273089/undefined-reference-to-uuid-generate-even-though-i-use-luuid

debugging make

make --just-print

https://www.oreilly.com/library/view/managing-projects-with/0596006101/ch12.html

Library mess

LD_LIBRARY_PATH is the runtime, at the compile time it searches with -L https://unix.stackexchange.com/questions/168340/where-is-ld-library-path-how-do-i-set-the-ld-library-path-env-variable

setting up in an environment variable?

#There is no env variable here, the best is to set CFLAGS or CXXFLAGS 
# a good make file should pick them up 
export CFLAGS="-L /home/atr/local/lib/ $CFLAGS"

Also other examples:

cc -o myprog obj1.o ... objn.o -Wl,-rpath=/path/to/lib \
   -L/path/to/lib -lmylib
gcc -luring -L/home/atr/local/lib/

https://www.hpc.dtu.dk/?page_id=1180 https://unix.stackexchange.com/questions/168340/where-is-ld-library-path-how-do-i-set-the-ld-library-path-env-variable

setting up non-standard include path

in bashrc

export CPATH=:/home/atr/local/include/:
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH 

# others 
$ export CPLUS_INCLUDE_PATH=${CPLUS_INCLUDE_PATH}:`pwd`/include
$ export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:`pwd`
$ export LIBRARY_PATH=${LIBRARY_PATH}:`pwd`
# see here: https://stackoverflow.com/questions/4250624/ld-library-path-vs-library-path 

https://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html

When compiling kernel, it can lead to

In file included from /usr/include/gelf.h:32,
                 from arch/x86/../../elf.h:22,
                 from arch/x86/decode.c:26:
/usr/include/libelf.h:46:4: error: unknown type name ‘Elf32_Word’
   46 |    Elf32_Word   ch_type;        /* Compression format.  */
      |    ^~~~~~~~~~

Then you must unset CPATH (https://www.linuxquestions.org/questions/linux-newbie-8/failed-to-build-linux-kernel-4-15-a-4175675058/)

Giving install path to make

make DESTDIR=/home/atr/local/ install

effect of $LD_LIBRARY_PATH

atr@atr-XPS-13:~/vu/github/animeshtrivedi/sapi-examples/src$ unset LD_LIBRARY_PATH 
atr@atr-XPS-13:~/vu/github/animeshtrivedi/sapi-examples/src$ gcc libiouring_copy.c -I /home/atr/local/include/ -luring -L /home/atr/local/lib/
atr@atr-XPS-13:~/vu/github/animeshtrivedi/sapi-examples/src$ ./a.out 
./a.out: error while loading shared libraries: liburing.so.2: cannot open shared object file: No such file or directory
atr@atr-XPS-13:~/vu/github/animeshtrivedi/sapi-examples/src$ ldd ./a.out 
	linux-vdso.so.1 (0x00007ffcccf7c000)
	libgtk3-nocsd.so.0 => /usr/lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007f2a415f2000)
	liburing.so.2 => not found
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2a41201000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f2a40ffd000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f2a40dde000)
	/lib64/ld-linux-x86-64.so.2 (0x00007f2a419fc000)
atr@atr-XPS-13:~/vu/github/animeshtrivedi/sapi-examples/src$ 

CMAKE include non-standard library and include path

arrow example: https://github.com/animeshtrivedi/benchmarking-arrow/blob/master/cpp/CMakeLists.txt

GCC/clang

gcc libiouring_copy.c -I /home/atr/local/include/ -luring -L /home/atr/local/lib/

sudo env

sudo env "PATH=$PATH;LD_LIBRARY_PATH=$LD_LIBRARY_PATH" zbd `