no member named 'hardware_constructive_interference_size' in namespace 'std' - c++

According to cppreference, to determine if std::hardware_constructive_interference_size is usable it uses the following example:
#include <new>
#ifdef __cpp_lib_hardware_interference_size
using std::hardware_constructive_interference_size;
using std::hardware_destructive_interference_size;
#else
// 64 bytes on x86-64 │ L1_CACHE_BYTES │ L1_CACHE_SHIFT │ __cacheline_aligned │ ...
constexpr std::size_t hardware_constructive_interference_size
= 2 * sizeof(std::max_align_t);
constexpr std::size_t hardware_destructive_interference_size
= 2 * sizeof(std::max_align_t);
#endif
However, my system defines __cpp_lib_hardware_interference_size but there is no symbol std::hardware_constructive_interference_size.
How can I handle this situation?
Is there a way to check if a symbol is defined?
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
macOS Catalina 10.15.7 (MacBook Pro 2019)
CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(untitled4)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
if (UNIX AND NOT APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif()
add_executable(untitled4 main.cpp)

How can I handle this situation?
You could detect the broken language implementation using pre-defined macros and make an exception for it.
Detection could be made by trying to compile and run a small program:
#include <new>
int main() {
#ifdef __cpp_lib_hardware_interference_size
// return 0 if the interference_sizes are defined
return !(std::hardware_constructive_interference_size &&
std::hardware_destructive_interference_size);
#else
return 1; // no interference_sizes
#endif
}
clang++ -std=c++17 -o hwisize hwisize.cpp 2>/dev/null && ./hwisize
has_hw_interference_sizes=$?
If the compilation fails, has_hw_interference_sizes will be 1.
If the compilation succeeds, but __cpp_lib_hardware_interference_size is not defined, has_hw_interference_sizes will be 1.
If the compilation succeeds, and __cpp_lib_hardware_interference_size is defined, has_hw_interference_sizes will be 0.
(The reversed bool logic is how common shells define true (0) and false (not 0))
Just plug that into your build system.

Related

CMake misinterpret types inside c++ program

Good day. Building program without using cmake
''' g++ -lpthread src/how2thread.cpp src/main.cpp -ggdb3 -std=c++17 -Wextra '''
produces no errors.
Although, using
'''
cmake . && make
'''
generates following build errors:
[ 25%] Building CXX object CMakeFiles/how2thread_lib.dir/src/how2thread.cpp.obj
D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.cpp: In member function 'how2thread::Request how2thread::Scheduler::get_request()':
D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.cpp:103:67: error: use of deleted function 'std::lock_guard<_Mutex>::lock_guard(const std::lock_guard<_Mutex>&) [with _Mutex = std::mutex]'
103 | auto m_lock = std::lock_guard<std::mutex>(data_arr_protect);
| ^
In file included from C:/msys64/mingw64/include/c++/12.1.0/mutex:43,
from D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.hpp:8,
from D:/Scizzors/Dsktp/cpp/how2thread/src/how2thread.cpp:4:
C:/msys64/mingw64/include/c++/12.1.0/bits/std_mutex.h:237:7: note: declared here
237 | lock_guard(const lock_guard&) = delete;
I specify in my header file, that data_arr_protect is std::mutex object
class Scheduler
{
// class for schedule slicers, processors and collecting data
public:
Scheduler(const std::vector<char>& data,
const std::string& mask); // construct and parse data
Request get_request(); // used to get another batch
void write_request(Request&&);
void write_finding(Finding&& find);
using Find_it = decltype(std::declval<std::vector<Finding>&>().cbegin());
Find_it cbegin();
Find_it cend();
const auto& get_slicing_status(){return is_slicing;} //
std::string mask;
#ifndef DEBUG_V
private:
#endif
const size_t slicer_num;
std::queue<Request> data_arr; // consider switching to deQ
std::mutex data_arr_protect;//for mt rw to vec // might be switching to semaphores
std::queue<Actor_processor> proc_instances_q;
std::queue<Actor_slicer> slicer_instances_q;
std::vector<Finding> result_arr;
std::mutex result_protect;//for mt write to vec
protected:
friend class Actor_slicer; // we need access from slicers to mutexes as they're pretty lowlvl
std::vector<std::mutex> char_segments_protect;
std::vector<size_t> finish_line_no;
std::atomic<size_t> is_slicing; // for break cond
};
My cmake file looks next
cmake_minimum_required(VERSION 3.10.0)
project(how2thread
VERSION 0.0.1
DESCRIPTION "simple mt parser app"
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
file(GLOB_RECURSE src_files src/*.cpp)
add_library(how2thread_lib src/how2thread.cpp src/how2thread.hpp)
add_executable(how2thread src/main.cpp)
target_link_libraries(how2thread PRIVATE how2thread_lib)
target_link_libraries(how2thread PRIVATE Threads::Threads)
What can cause compiler's misinterpretation and how i must fix it?
I tried different linking options, as building my how2thread.cpp file with main.cpp file, although it didn't help. And I do not get errors about undefined class or something, so, I guess, header file being included by .cpp one
Nothing is being misinterpreted. The issue is the following line: (Which appears only in the error message, not the code you show. Please always provide a minimal reproducible example!)
auto m_lock = std::lock_guard<std::mutex>(data_arr_protect);
This is copy-initialization of m_lock from the initializer expression which before C++17 required the type to be move-constructible, which std::lock_guard is not. Direct-initialization with auto (e.g. auto m_lock(std::lock_guard<std::mutex>(data_arr_protect));) would also not work for the same reason.
To compile this line you need to use C++17 or later, but you are asking cmake to set C++14 with
set(CMAKE_CXX_STANDARD 14)
So change the 14 to 17.
Or if you require compatibility with C++14, you will have to avoid the auto initialization idiom for non-movable types:
std::lock_guard<std::mutex> m_lock(data_arr_protect);

-Wundef does not warn about an undefined symbol in front of #ifdef

Please consider the following code:
// program.cpp
#include <iostream>
int main() {
#ifdef LINUX
std::cout << "Linux\n";
#elif MAC
std::cout << "Mac\n";
#elif WINDOWS
std::cout << "Windows\n";
#elif BSD
std::cout << "BSD\n";
#else
std::cout << "Something else\n";
#endif
return 0;
}
If I compile it with both clang and gcc,
clang++ -Wundef -DBSD -o program program.cpp
# or
g++ -Wundef -DBSD -o program program.cpp
I will get warnings for not defining symbols MAC and WINDOWS, but no warning for the symbol LINUX:
program.cpp:6:7: warning: 'MAC' is not defined, evaluates to 0 [-Wundef]
#elif MAC
^
program.cpp:8:7: warning: 'WINDOWS' is not defined, evaluates to 0 [-Wundef]
#elif WINDOWS
^
2 warnings generated.
According to the gcc man page:
-Wundef
Warn if an undefined identifier is evaluated in an "#if" directive.
Such identifiers are replaced with zero.
It says in an #if directive. Is it because the LINUX is not inside that structure?
If that's the case, how can I tell compiler to emit warnings for the undefined symbol LINUX?
clang version 12.0.1
gcc (GCC) 11.1.0
Target: x86_64-pc-linux-gnu (artixlinux)
The reason is that your preprocessor code asks if LINUX is defined. But for MAC, WINDOWS and BSD you don’t bother checking whether the symbol is defined; instead, your code assumes it is defined and asks for its value.
Change your code to use #elif defined(…) instead of #elif … to fix the warning.

Magick++ undefined reference to Magick::Image::Columns

Good day,
here is my code
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv) {
InitializeMagick(*argv);
Image image;
try {
image.read(argv[1]);
}
catch( Exception &error_ ) {
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
int x = image.columns();
cout<<"your picture's width is "<< x << "px"<<endl;
return 0;
}
I use KDevelop(which uses CMake as builder),
when I try to compile the app, it throws me an error
main.cpp:25: undefined reference to `Magick::Image::columns() const'
Here's what my CMakeLists.txt contains.
cmake_minimum_required(VERSION 3.5)
project(hello)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(hello ${SOURCE_FILES})
add_definitions( -DMAGICKCORE_QUANTUM_DEPTH=16 )
add_definitions( -DMAGICKCORE_HDRI_ENABLE=0 )
find_package(ImageMagick COMPONENTS Magick++)
include_directories(${ImageMagick_INCLUDE_DIRS})
target_link_libraries(hello ${ImageMagick_LIBRARIES})
I figured out that there're often issues with undefined references when CMakeLists isn't written correctly, but I made it according to this About Magick++, how to write the CMakeLists?
where am I wrong? I can add any information needed.
UPD 1.
version of magick++,
8:6.8.9.9-7ubuntu5.7
system info:
Description: Linux Mint 18.1 Serena
UPD 2.
I just removed parenthesis and when tryed to compile with
size_t x = image.columns;
size_t y = image.rows;
KDevelop threw me
main.cpp:25:22: error: cannot convert ‘Magick::Image::columns’ from type ‘size_t (Magick::Image::)() const {aka long unsigned int (Magick::Image::)() const}’ to type ‘size_t {aka long unsigned int}’
even when
auto x = image.columns;
auto y = image.rows;
it throws
main.cpp:25:20: error: cannot convert ‘Magick::Image::columns’ from
type ‘size_t (Magick::Image::)() const {aka long unsigned int
(Magick::Image::)() const}’ to type ‘long unsigned int
(Magick::Image::*)() const’
what's happening?
P.S. hooray, this is my first question on stackoverflow! :-)
If you are able to compile your program without CMake using g++ main.cpp `Magick++-config --cxxflags --cppflags --ldflags --libs` (but for some reason cannot use ${ImageMagick_LIBRARIES} in CMake), then you can make use of Magick++-config in your CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(hello LANGUAGES CXX)
add_executable(hello main.cpp)
target_compile_features(hello PRIVATE cxx_std_11)
find_package(ImageMagick REQUIRED COMPONENTS Magick++)
target_compile_definitions(hello PRIVATE
MAGICKCORE_QUANTUM_DEPTH=16
MAGICKCORE_HDRI_ENABLE=0
)
target_include_directories(hello PRIVATE ${ImageMagick_INCLUDE_DIRS})
execute_process(COMMAND Magick++-config --ldflags
OUTPUT_VARIABLE ImageMagick_LINK_FLAGS
OUTPUT_STRIP_TRAILING_WHITESPACE
)
target_link_libraries(hello PRIVATE ${ImageMagick_LINK_FLAGS})
Here, execute_process allows us to get the result of Magick++-config --ldflags into a variable, which we can pass as flags to the linker through target_link_libraries.
Also, note how I've used target_compile_features rather than setting the global CMAKE_CXX_FLAGS variable, target_compile_definitions rather than add_definitions and target_include_directories rather than include_directories. It's better to use local (target-based) commands rather than modifying global state, both in programming and in CMake, since they can have unforeseen repercussions down the line -- in the context of CMake, those global commands would have affected nested sub-projects.
ForgottenUbrella 's version which I adapted didn't quite work for me, copying a line in from another project fixed it. Note, I'm using c++20 not 11.
I had the following error:
..... undefined reference to symbol 'pthread_create##GLIBC_2.2.5'
and the line that fixed it:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++2a -pthread")

Easylogging 8.91 not compling without c++11

I want to use the library without C++11 but it won't compile for me:
(Theoretically it should as per documentation #http://easylogging.muflihun.com:
"For lower version of C++ (non-C++11), please consider using Easylogging++ v8.91. ")
error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
File structure:
./Main.cpp
./logger/easylogging++.h
Contents of Main.cpp:
#include "logger/easylogging++.h"
_INITIALIZE_EASYLOGGINGPP
using namespace std;
int main(int argc, char* argv[]) {
LINFO << "This is my first log";
return 0;
}
../src/logger/easylogging++.h: In function ‘std::string easyloggingpp::internal::threading::getCurrentThreadId()’:
../src/logger/easylogging++.h:691:16: error: ‘std::this_thread’ has not been declared
ss << std::this_thread::get_id();
Compiler: gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1), OS: Ubuntu 14.04 LTS
As T.C. suggested in the solution is to change this section of code at the top of easylogging++.h:
#if defined(__GNUC__)
# define _ELPP_GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
# if defined(__GXX_EXPERIMENTAL_CXX0X__)
# define _ELPP_CXX0X 1
# elif (_ELPP_GCC_VERSION >= 40801)
# define _ELPP_CXX11 1
# endif // defined(__GXX_EXPERIMENTAL_CXX0X__)
#endif // defined(__GNUC__)
Changing both _ELPP_CXX0X and _ELPP_CXX11 to 0 will fix the issue.

CUDA compilation issue with CMake

I am having issues with compiling my CUDA code with CMake. I am using CUDA 7 and the version information from nvcc is as follows:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2014 NVIDIA Corporation
Built on Tue_Dec__9_18:10:46_CST_2014
Cuda compilation tools, release 7.0, V7.0.17
My CMake file uses the find_cuda macro as follows:
find_package(CUDA)
if(CUDA_FOUND)
list(APPEND CUDA_NVCC_FLAGS "-arch=sm_20;--compiler-options;-std=c++11;-O2;-DVERBOSE")
endif(CUDA_FOUND)
I added the std=c++11 compiler flag after many posts suggested this was needed. However, I get exactly the same errors with or without this flag.
I also added the following to remove the C++11 support from nvcc compilation flags but this does not change anything either.
if(CMAKE_COMPILER_IS_GNUCC)
string(REPLACE "-std=c++11" "" CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}")
string(REPLACE "-std=c++0x" "" CUDA_HOST_FLAGS "${CUDA_HOST_FLAGS}")
endif(CMAKE_COMPILER_IS_GNUCC)
The errors I get are as follows:
/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: identifier "nullptr" is undefined
/usr/lib/gcc/x86_64-linux-gnu/4.8/include/stddef.h(432): error: expected
a ";"
/usr/include/x86_64-linux-gnu/c++/4.8/bits/c++config.h(190): error:
expected a ";"
/usr/include/c++/4.8/exception(63): error: expected a ";"
/usr/include/c++/4.8/exception(68): error: expected a ";"
/usr/include/c++/4.8/exception(76): error: expected a ";"
/usr/include/c++/4.8/exception(83): error: expected a ";"
/usr/include/c++/4.8/exception(93): error: expected a "{"
/usr/include/c++/4.8/bits/exception_ptr.h(64): error: function
"std::current_exception" returns incomplete type
"std::__exception_ptr::exception_ptr"
I am using gcc 4.8 but get the same errors with 4.7 as well. I am on cmake 2.8.12.2.
Compiling with CMAKE verbose gives the following flags for nvcc compilation:
/usr/local/cuda-7.0/bin/nvcc /home/xargon/Dropbox/code/gpu-mosaicing
/src/gpu/kernels/bgra_2_gray.cu -c -o /home/xargon/code/mosaicing_bin
/gpu/kernels/CMakeFiles/kernels.dir//./kernels_generated_bgra_2_gray.cu.o
-ccbin /usr/bin/cc -m64 -DUSE_CUDA -DUSE_OPENCV -DUSE_QT -Xcompiler
,\"-std=c++11\",\"-O3\",\"-DNDEBUG\" -arch=sm_20 --compiler-options
-std=c++11 -O2 -DVERBOSE -DNVCC -I/usr/local/cuda-7.0/include -I/usr/local
/include/opencv -I/usr/local/include -I/home/xargon/Dropbox/code/gpu-
mosaicing/src/cpu/gui/qt -I/usr/include -I/home/xargon/Dropbox/code/gpu-
mosaicing/src/cpu/core -I/home/xargon/Dropbox/code/gpu-mosaicing/src/cpu
/datasources -I/home/xargon/Dropbox/code/gpu-mosaicing/src/gpu
/intraoperability -I/home/xargon/Dropbox/code/gpu-mosaicing/src/utils
-I/usr/local/cuda-7.0/include
This worked for me using CUDA 7, gcc 4.8.2 and CMake 3.0.2.
I updated the code and added a simple thrust-based example to make it clear that you can use C++11 in CUDA code
CMakeLists.txt
project(cpp11)
find_package(CUDA)
list(APPEND CUDA_NVCC_FLAGS "-arch=sm_20;-std=c++11;-O2;-DVERBOSE")
SET(CUDA_PROPAGATE_HOST_FLAGS OFF)
CUDA_ADD_EXECUTABLE(cpp11 main.cpp test.h test.cu)
test.h
#ifndef TEST_H
#define TEST_H
int run();
#endif
test.cu
#include "test.h"
#include <thrust/device_vector.h>
#include <thrust/reduce.h>
#include <thrust/sequence.h>
template<typename T>
struct Fun
{
__device__ T operator()(T t1, T t2)
{
auto result = t1+t2;
return result;
}
};
int run()
{
const int N = 100;
thrust::device_vector<int> vec(N);
thrust::sequence(vec.begin(),vec.end());
auto op = Fun<int>();
return thrust::reduce(vec.begin(),vec.end(),0,op);
}
main.cpp
#include <iostream>
#include "test.h"
int main()
{
std::cout << run() << std::endl;
return 0;
}
list(APPEND CUDA_NVCC_FLAGS "-std=c++11") is enough,SET(CUDA_PROPAGATE_HOST_FLAGS OFF) may be not necessary, and it cause me could not set breakpoint in .cu file
If stumbling across this question while searching for a way to compile Genoils CPP-Ethereum build for Ethereum CUDA mining, my problem was solved by editing the CMakeLists.txt file in the cpp-ethereum/libethash-cuda folder.
Where it states:
set(CUDA_NVCC_FLAGS
${CUDA_NVCC_FLAGS};
-gencode etc etc)
add "-std=c++11" after the semi-colon, as follows:
set(CUDA_NVCC_FLAGS
${CUDA_NVCC_FLAGS};
-std=c++11
-gencode etc etc)