Some useful C++ code snippets
to_string is not a member of std, says g++ (mingw)
g++ xxx.cpp -std=c++11
This solves the error:
/usr/include/c++/5/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support \
g++ xxx.cpp -I /usr/local/include
cmake_minimum_required(VERSION 3.5)
project(name)
# specify the c++ version to use
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-result -Wno-unknown-pragmas -Wfatal-errors -fPIC -std=c++11")
# the compiler flags for compiling C sources
message(STATUS "CMAKE_C_FLAGS: " ${CMAKE_C_FLAGS})
# the compiler flags for compiling C++ sources
message(STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS})
find_package(OpenCV REQUIRED)
set(SOURCE_FILES ./src/file1.cpp ./src/file2.cpp)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
link_directories(${CMAKE_SOURCE_DIR}/libs)
# create a executable file
add_executable(<executable_file_name> ./src/main.cpp)
target_link_libraries(<executable_file_name> ${OpenCV_LIBS})
# create a shared object
add_library(<library_name> SHARED ${SOURCE_FILES})
target_link_libraries(<library_name> ${OpenCV_LIBS})
Ref: Configuring an c++ OpenCV project with Cmake
CMake: Output a list with delimiters
set(VAR1 hello)
set(LIST1 a b c d)
message("the variable VAR1 is " ${VAR1})
message("the list LIST1 is " "${LIST1}")
Other types of variable: What's the CMake syntax to set and use variables?
This equals to add #define MYVAR
in .cpp
or .h
files.
add_definitions(-D MYVAR)
Are CMAKE_SOURCE_DIR and PROJECT_SOURCE_DIR the same in CMake?
CMAKE_SOURCE_DIR
: the folder containing the top-level CMakeLists.txt
PROJECT_SOURCE_DIR
: the inner-most folder containing CMakeLists.txt with project()
command
make -j $(nproc)
Using CMake with GNU Make: How can I see the exact commands?
cmake -DCMAKE_BUILD_TYPE=DEBUG ..
make VERBOSE=1 2>../log.txt
The 2>../log.txt
redirect error message into a log file.
ldd <executable>
Sample output:
libudev.so.1 => /lib/x86_64-linux-gnu/libudev.so.1 (0x00007fc3364fd000)
libsoxr.so.0 => /usr/lib/x86_64-linux-gnu/libsoxr.so.0 (0x00007fc336297000)
libnuma.so.1 => /usr/lib/x86_64-linux-gnu/libnuma.so.1 (0x00007fc33608c000)
nm -D <xxx.so> # -D will list dynamic symbols
Sample output:
0000000000092030 T YoloPluginCtxInit
U rename
0000000000104d8f V _ZTS4Yolo
000000000007c425 W _ZSt10_ConstructI7DsImageIEEvPT_DpOT0_
T
for symbol defined in code section, U
for undefined, V
and W
for weak object.
This is helpful for solving the following error:
/home/ubuntu/catkin_ws/devel/lib/ros_trans/OpenTrans: symbol lookup error: /home/ubuntu/catkin_ws/devel/lib/libproduct.so: undefined symbol: YoloPluginGetVersion
c++filt <symbol(mangled name)>
Error message while linking:
/usr/bin/ld: cannot find -lcudnn
collect2: error: ld returned 1 exit status
Solution: https://stackoverflow.com/questions/51451746/cmake-command-line-ld-library-path-and-c-include-dirs
First set LD_LIBRARY_PATH
to the path of that .so
file, and then add:
link_directories( $ENV{LD_LIBRARY_PATH} )
into CMakeLists.txt
.
./a.out: error while loading shared libraries: xxx.so: cannot open shared object file: No such file or directory
g++ executable cannot find shared library
LD_LIBRARY_PATH=/usr/local/lib ./a.out
or
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
From StackOverflow - Stack smashing detected, add the following in CMakeLists
to temporarily get rid of this error:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-stack-protector -std=c++11")
make -j 8 g++: internal compiler error: Killed (program cc1plus)
That's probably make -j4
uses to much memory, so it's killed by system, try using make -j1
instead.
Another method is to tell kswapd0 to only move stuff to SWAP when you are completely out of RAM(kswapd0 is taking a lot of cpu):
echo vm.swappiness=0 | sudo tee -a /etc/sysctl.conf
Method 1, use -D
flag while cmake
:
cmake -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda ..
Method 2, revise CMakeLists.txt
, before FIND_PACKAGE(CUDA)
, add one line, this becomes:
set(CUDA_TOOLKIT_ROOT_DIR /usr/local/cuda)
FIND_PACKAGE(CUDA)
set(OpenCV_DIR /usr/local/lib/cmake/opencv4) # set to system installation directory
set(OpenCV_DIR ~/installation/opencv-4.1.2/build) # set to opencv build directory
FIND_PACKAGE(OpenCV REQUIRED)
Another method: cmake find_package specify path
Separating class code into a header and cpp file
Preprocessor check if multiple defines are not defined
#define <MACRO_A>
#ifdef <MACRO_A>
#elif defined(<MACRO_B>)
#elif !define(<MACRO_C>) && !define(<MACRO_D>)
#elif !define(<MACRO_E>) || !define(<MACRO_F>)
#else
#end
#ifndef __SOMEFILE_H__
#define __SOMEFILE_H__
// ...
#endif
can be replaced with:
#pragma once
// ...