Use Tbb library in Clion IDE - c++

I am trying run this code in Clion IDE. I want to use TBB library. I use C++ 11.
#include “tbb/blocked_range.h”
#include “tbb/parallel_for.h”
#include <vector>
const size_t SIZE = 10000000;
int main()
{
std::vector<double> myArray(SIZE);
tbb::parallel_for(tbb::blocked_range<size_t>(0, SIZE),
[&myArray](const tbb::blocked_range<size_t> &r)
{
for (size_t i = r.begin(); i != r.end(); i++)
Calculate(myArray[i]);
});
return 0;
}
from command line :
g++ task1.cpp -o task1 -std=c++11 -fopenmp -ltbb
this code work perfect.
But in Clion not.
I think problem in my CMakeLists.txt file.
What Clion tell me:
CMakeFiles/parallel.dir/main.cpp.o: In function `run':
/usr/include/tbb/parallel_reduce.h:148: undefined reference to `tbb::task_group_context::~task_group_context()'
/usr/include/tbb/parallel_reduce.h:148: undefined reference to `tbb::task_group_context::~task_group_context()'
CMakeFiles/parallel.dir/main.cpp.o:(.rodata+0xa0): undefined reference to `typeinfo for tbb::task'
CMakeFiles/parallel.dir/main.cpp.o:(.rodata+0x1c8): undefined reference to `tbb::task::note_affinity(unsigned short)'
CMakeFiles/parallel.dir/main.cpp.o: In function `tbb::task_group_context::task_group_context(tbb::task_group_context::kind_type, unsigned long)':
/usr/include/tbb/task.h:441: undefined reference to `tbb::task_group_context::init()'
...
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(parallel)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp -ltbb")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp main.cpp)
add_executable(parallel ${SOURCE_FILES})
How can I change my CMakeLists.txt file or I should do some else? Thanks for you help.

First, you have to make sure your TBB is setup correctly.
The current version of TBB is oneTBB. It is an open source project supported by Intel. The older version of TBB is here, but it sends you to the newer version, oneTBB. So, it is better to stay with the newer oneTBB.
There are two ways to get oneTBB: Either from the oneTBB Github source here (which is explained here) or from Intel oneTBB here.
I preferred Intel, since it provides a GUI installer (also provides apt for Linux). After installing oneTBB using the GUI, you have to set the environment which is explained here.
You should test you oneTBB settings by a simple code. Above web page here provides a sample code, which I wrapped with a main function for your convenience (note the included header oneapi/tbb.h):
test.cpp
#include <iostream>
#include "oneapi/tbb.h"
int main(){
int sum = oneapi::tbb::parallel_reduce(oneapi::tbb::blocked_range<int>(1,101), 0,
[](oneapi::tbb::blocked_range<int> const& r, int init) -> int {
for (int v = r.begin(); v != r.end(); v++ ) {
init += v;
}
return init;
},
[](int lhs, int rhs) -> int {
return lhs + rhs;
}
);
std::cout << sum << '\n';
return 0;
}
Compile it with (as explained here):
g++ -o test test.cpp $(pkg-config --libs --cflags tbb)
If your system (gcc, oneTBB and others) are setup properly, this should work.
When it comes to CLion, you will need CMakeLists.txt file. There are examples in the oneTBB GitHub page here. Open for example test_all folder with CLion as if you are opening a project. It should build from there. By looking at the cmake files of the example project, you can create you own cmake file.
Update:
Please look at this post and posts there from Intel for oneTBB.
I have prepared sample files for old TBB and oneTBB here for building with both command line GCC and CMAKE. CMAKE is the one required for CLion.
For CLion and the old version of TBB this would be a basic CMAKE file:
cmake_minimum_required(VERSION 3.xx)
project(testtbb)
set(CMAKE_CXX_STANDARD 17)
find_package(TBB REQUIRED)
add_executable(testtbb_long.out testtbb_long.cpp)
target_link_libraries(testtbb_long.out tbb)
Note that for linking the TBB library to the defined executable, you need to use target_link_libraries as:
target_link_libraries(executable-name.out tbb)

You should use a cmake test to find libtbb, which also sets the correct variables.
There is a FindTBB on github, which you can try to use.

Related

wxWidgets runtime error (Mismatch version)

I have a problem at start the program:
Fatal Error: Mismatch between the program and library build versions detected.
The library used 3.0 (wchar_t,compiler with C++ ABI 1010,wx containers,compatible with 2.8),
and your program used 3.0 (wchar_t,compiler with C++ ABI 1009,wx containers,compatible with 2.8).
My cmake settings:
cmake_minimum_required(VERSION 3.0)
project(simple)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${wxWidgets_CXX_FLAGS} -Wall -std=c++14")
find_package(wxWidgets COMPONENTS net gl core base)
include("${wxWidgets_USE_FILE}")
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} ${wxWidgets_LIBRARIES})
Version of wxWidgets 3.0.3.
If your desire is to have __GXX_ABI_VERSION=1002, specify -fabi-version=2 to GCC. To do this in your CMakeLists.txt, add:
add_definitions(-fabi-version=2)
This is a preferred approach compared to manually redefining __GXX_ABI_VERSION, which would violate C++ standards and potentially cause undefined behavior.
Note: -fabi-version=2 may not always correspond to __GXX_ABI_VERSION=1002 in future releases of GCC. Compile and run this quick C++ program to check it:
#include <iostream>
int main(void) {
std::cout << "__GXX_ABI_VERSION=" << __GXX_ABI_VERSION << std::endl;
return 0;
}
Compile this way:
g++ -fabi-version=2 -o check_fabi_version check_fabi_version.cpp
Run this way:
./check_fabi_version
Example output as of GCC 8.2.0:
__GXX_ABI_VERSION=1002
You can try to add to your program
#define __GXX_ABI_VERSION 1010
or just
sudo apt-get purge wx2.8-headers wx2.9-headers
I had two version of wxWidgets instaled. I deleted one of them and it works great.

Can clang static analyzer (scan-build) be used with cmake --build?

I'd like to use the clang static analyzer command line tool scan-build with the convenience of cmake --build.
Most of the resources I found online seem to indicate you need a two-step process:
scan-build cmake .
scan-build make
For example for this small example program with a bug that scan-build catches:
#include <iostream>
int fun() {
int x;
return x; # main.cpp:5:5: warning: Undefined or garbage value returned to caller
}
int main() {
int a = fun();
std::cout << "Hello, World! " << a << std::endl;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(test_program)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(test_program ${SOURCE_FILES})
If I run scan-build cmake --build cmake-build-release/ it does not find the bug, even though the binary is built. Is there anyway to get scan-build to work in a one step process with CMake?
If you want to use the Clang Static Analyzer, you should just set CMAKE_EXPORT_COMPILE_COMMANDS=YES. This will create a compilation database that CSA can read. You don't even need to build your project. The file is located at: /path/to/build/compile_commands.json.
scan-build is designed for projects that can't create a compilation database themselves.
Then you can run:
analyze-build --cdb /path/to/build/compile_commands.json \
--use-analyzer /path/to/clang \
--output /path/to/output
It's worth noting that clang-tidy has all of the CSA checks now. You can use this same compilation database technique to run clang-tidy on your codebase.

OS X/Clang won't use c++11 headers

system info: OS X 10.10.5, Clang = Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn), cmake = 2.8.12.2
Suppose I have some simple file, main.cpp:
#include <stdio.h>
#include <vector>
#include <algorithm>
int main(void)
{
std::vector<int> v{1, 2, 3, 4};
int sum = std::accumulate(v.begin(), v.end(), 0);
printf("Sum = %d\n", sum);
return 0;
}
When I run "clang++ -stdlib=libc++ -std=c++11 main.cpp" I get error:
main.cpp:11:20: error: no member named 'accumulate' in namespace 'std'
int sum = std::accumulate(v.begin(), v.end(), 0);
When I look, using an IDE (Qt Creator), I see that the included header is /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/algorithm
when I look through my file system, I see that /usr/include/c++/4.2.1 exists with c++11 compliant headers.
Next, I use cmake to control larger builds (the above is just an example setup).
So here's a token CMakeLists.txt file for the above example:
project(c11Test)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_FLAGS "-stdlib=libc++ -std=gnu++11")
add_executable(${PROJECT_NAME} main.cpp)
Which, when I build, creates this output (excerpt):
[100%] Building CXX object CMakeFiles/c11Test.dir/main.cpp.o
/usr/bin/c++ -stdlib=libc++ -std=gnu++11 -o
CMakeFiles/c11Test.dir/main.cpp.o -c /Users/username/c11Test/main.cpp
/Users/username/c11Test/main.cpp:11:20: error: no member named
'accumulate' in namespace 'std'
int sum = std::accumulate(v.begin(), v.end(), 0);
~~~~~^ 1 error generated.
(whole output here):
I'm aware of this post, which seems to imply that all I need to do is just include these compiler flags. But that doesn't seem to work.
Furthermore, I probably need to build this on a variety of OS X computers at different versions, so I really wonder if there's a general solution I'm overlooking here?
Another friend provided the answer:
In my bigger program (not here posted, obv.) Something had included <numeric> in some kind of hidden way. When I went to compile on OSX, it was no longer included. I had thought <algorithm> was the necessary component, but clearly it wasn't. So just my error.

Can't link program using Boost.Filesystem

I'm trying to run program, using sample code of boost::filesystem on Ubuntu 12.10, but it doesn't want to build.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace std;
void fun(const string& dirPath);
int main()
{
fun("/home");
return 0;
}
void fun(const string& dirPath)
{
path p (dirPath);
if (exists(p))
{
if (is_regular_file(p))
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p))
cout << p << "is a directory\n";
else
cout << p << "exists, but is neither a regular file nor a directory\n";
}
else
cout << p << "does not exist\n";
}
And CMake code:
project(tttest)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
FIND_PACKAGE(Boost 1.53 COMPONENTS filesystem system REQUIRED)
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES})
Unfortunately it generates errors
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::exists(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem6existsERKNS0_4pathE[_ZN5boost10filesystem6existsERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::is_directory(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem12is_directoryERKNS0_4pathE[_ZN5boost10filesystem12is_directoryERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::is_regular_file(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem15is_regular_fileERKNS0_4pathE[_ZN5boost10filesystem15is_regular_fileERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::status(boost::filesystem::path const&, boost::system::error_code*)'
CMakeFiles/tttest.dir/main.cpp.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
main.cpp:(.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[_ZN5boost10filesystem9file_sizeERKNS0_4pathE]+0x19): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
collect2: error: ld returned 1 exit status
What is the reason of this problem and how to solve it?
Boost filesystem is one of the Boost library that have some ABI problem relative to function signature change due to C++0x or C++11. cf Boost ticket : https://svn.boost.org/trac/boost/ticket/6779
You have three solutions:
Inhibit C++11 scoped enums in concerned Boost header files included in your programs with #include (cf http://www.ridgesolutions.ie/index.php/2013/05/30/boost-link-error-undefined-reference-to-boostfilesystemdetailcopy_file/):
#define BOOST_NO_CXX11_SCOPED_ENUMS
#include <boost/filesystem.hpp>
#undef BOOST_NO_CXX11_SCOPED_ENUMS
But this solution is not a complete one and I read it does not work for everybody.
Build BOOST with the C++11 option (the same options you use for your application): http://hnrkptrsn.github.io/2013/02/26/c11-and-boost-setup-guide
I read also it does not work for everybody.
Set up a cross compiler dedicated to your application where you rebuild all the libraries you need in a dedicated environment. That ensures coherence plus stability plus more maintainability, and is certainly the solution to recommend.
I have not read if it has been tested - probably yes, and probably it works. Anyway, cross compiling is well mastered now in computer science. You will find many good tutorials and support for it. In Linux Gentoo, they have the marvelous sys-devel/crossdev package that makes it very easy.
In my own case, solution 1 has solved the problem. As soon as I encounter another one, I will switch to solution 3. So, I have not yet tested it.
You need to add libboost_filesystem library when linking. Or libboost_filesystem-mt if your application is multi-threaded. Like this:
g++ -o file -lboost_filesystem-mt source_file.cpp
The solution that worked for me is to compile with "-c" and then create the executable like this:
g++ -c -o main.o main.cpp
g++ -o my_prog main.o -lboost_system -lboost_filesystem
For some boost modules, you have to compile libraries and link them (using bootstrap.sh).
In your case, you have to compile and link Filesystem, and probalbly System too
Have a look here
For example:
./bootstrap.sh (bjam)
rm -rf bin.v2 stage (between 2 bjam commands)
./bjam release toolset=gcc address-model=64 cxxflags=-fPIC
./bjam debug toolset=gcc address-model=64 cxxflags=-fPIC
If you are linking on Windows, you don't have to manually link your libraries, since they are automatically linked using pragma. On Linux, you have to do it.
According to documentation, these modules need you to acquire or build a library :
Boost.Filesystem
Boost.GraphParallel
Boost.IOStreams
Boost.MPI
Boost.ProgramOptions
Boost.Python
Boost.Regex
Boost.Serialization
Boost.Signals
Boost.System
Boost.Thread
Boost.Wave
You need to add the following libraries:
g++ -o file -lboost_system -lboost_filesystem sourcefile.cpp
If you use a Makefile:
CC=gcc
CXX=g++
PROG = program
CXXFLAGS := -std=c++1y -g -Wall
LDFLAGS = -L/usr/local/lib -L/usr/lib/x86_64-linux-gnu
LIBS= -lboost_system -lboost_filesystem
SRCS= main.cpp
OBJS=$(subst .cpp,.o,$(SRCS))
all: $(OBJS)
$(CXX) $(CXXFLAGS) -o $(PROG) $(OBJS) $(LIBS) $(LDFLAGS)

C++ Boost: undefined reference to boost::system::generic_category()

I am trying to include Boost libraries in my project and have been facing issues in the same. I am on Ubuntu 12.10 with Codeblocks IDE and tried installing the libraries manually reading instructions from the site, but was getting error's with header as well as to-be-built-before-use libraries.
I then installed the libraries via terminalby sudo apt-get install libboost-all-dev. After this, in my programs on Codeblocks, I can include headers like #include <boost/regex.hpp> but when I try to include the header for the Filesystem library ( #include "boost/filesystem/operations.hpp" ), I am getting the following error:
/usr/include/boost/system/error_code.hpp|214|undefined reference to boost::system::generic_category()'|
I am not sure how to resolve this error (specifically in Codeblocks on Linux). I really could use some help here.
Compiler : Gcc
Program code: Only tried inlcuding the above file system operations.hpp file.
Build log from Codeblocks:
Build started on: 20-11-2012 at 18:02.53
Build ended on: 20-11-2012 at 18:02.54
-------------- Build: Debug in libopenFrameworks ---------------
Target is up to date.
-------------- Build: Debug in reader1 ---------------
make -s -f Makefile Debug
linking i686 bin/reader1_debug linux
obj/i686Debug/src/testApp.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
obj/i686Debug/src/main.o: In function `__static_initialization_and_destruction_0':
/usr/include/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
/usr/include/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
collect2: ld returned 1 exit status
make: *** [bin/reader1_debug] Error 1
Process terminated with status 2 (0 minutes, 1 seconds)
6 errors, 0 warnings
You should link in the libboost_system library. I am not sure about codeblocks, but the g++ command-line option on your platform would be
-lboost_system
Depending on the boost version libboost-system comes with the -mt suffix which should indicate the libraries multithreading capability.
So if -lboost_system cannot be found by the linker try -lboost_system-mt.
This answer actually helped when using Boost and cmake.
Adding add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY) for cmake file.
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.12)
project(proj)
set(CMAKE_CXX_STANDARD 17)
set(SHARED_DIR "${CMAKE_SOURCE_DIR}/../shared")
set(BOOST_LATEST_DIR "${SHARED_DIR}/boost_1_68_0")
set(BOOST_LATEST_BIN_DIR "${BOOST_LATEST_DIR}/stage/lib")
set(BOOST_LATEST_INCLUDE_DIR "${BOOST_LATEST_DIR}/boost")
set(BOOST_SYSTEM "${BOOST_LATEST_BIN_DIR}/libboost_system.so")
set(BOOST_FS "${BOOST_LATEST_BIN_DIR}/libboost_filesystem.so")
set(BOOST_THREAD "${BOOST_LATEST_BIN_DIR}/libboost_thread.so")
set(HYRISE_SQL_PARSER_DIR "${SHARED_DIR}/hyrise_sql_parser")
set(HYRISE_SQL_PARSER_BIN_DIR "${HYRISE_SQL_PARSER_DIR}")
set(HYRISE_SQL_PARSER_INCLUDE_DIR "${HYRISE_SQL_PARSER_DIR}/src")
set(HYRISE_SQLPARSER "${HYRISE_SQL_PARSER_BIN_DIR}/libsqlparser.so")
include_directories(${CMAKE_SOURCE_DIR} ${BOOST_LATEST_INCLUDE_DIR} ${HYRISE_SQL_PARSER_INCLUDE_DIR})
set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu/")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY)
find_package(Boost 1.68.0 REQUIRED COMPONENTS system thread filesystem)
add_executable(proj main.cpp row/row.cpp row/row.h table/table.cpp table/table.h page/page.cpp page/page.h
processor/processor.cpp processor/processor.h engine_instance.cpp engine_instance.h utils.h
meta_command.h terminal/terminal.cpp terminal/terminal.h)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(proj PUBLIC Boost::system Boost::filesystem Boost::thread ${HYRISE_SQLPARSER})
endif()
It's a linker problem. Include the static library path into your project.
For Qt Creator open the project file .pro and add the following line:
LIBS += -L<path for boost libraries in the system> -lboost_system
In my case Ubuntu x86_64:
LIBS += -L/usr/lib/x86_64-linux-gnu -lboost_system
For Codeblocks, open up Settings->Compiler...->Linker settings tab and add:
boost_system
to the Link libraries text widget and press OK button.
I searched for a solution as well, and none of the answers I encountered solved the error, Until I found the answer of "ViRuSTriNiTy" to this thread: Undefined reference to 'boost::system::generic_category()'?
according to that answer, try to add these lines to your cmake file:
find_package(Boost 1.55.0 REQUIRED COMPONENTS system filesystem)
include_directories(... ${Boost_INCLUDE_DIRS})
link_directories(... ${Boost_LIBRARY_DIRS})
target_link_libraries(... ${Boost_LIBRARIES})
Same problem on building a simple boost example, solved after i changed the g++ compiler flag from -std=c++14 to -std=c++11.
And I noticed that it's a C++11 Example...
I had the same problem and also use Linux Mint (as nuduoz) . I my case problem was solved after i added boost_system to GCC C++ Linker->Libraries.
You could come across another problem. After installing Boost on the Linux Mint I've had the same problem. Linking -lboost_system or -lboost_system-mt haven't worked because library have had name libboost_system.so.1.54.0.
So the solution is to create symbolic link to the original file. In my case
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_system.so.1.54.0 /usr/lib/libboost_system.so
For more information see this question.
g++ -lboost_system -lboost_filesystem userentry.cpp -o userentry
worked perfectly under debian. (boost c++ libraries installed with apt-get).
Il the library is not installed you should give boost libraries folder:
example:
g++ -L/usr/lib/x86_64-linux-gnu -lboost_system -lboost_filesystem prog.cpp -o prog
try
g++ -c main.cpp && g++ main.o /usr/lib/x86_64-linux-gnu/libboost_system.so && ./a.out
/usr/lib/x86_64-linux-gnu/ is the location of the boost library
use find /usr/ -name '*boost*.so' to find the boost library location
In my project I prefer to use header-only libraries. So nothing above was helpful. What did really help is:
add_definitions(-DBOOST_SYSTEM_NO_DEPRECATED)
After testing the proposed solutions described above, I found only these few of lines would work.
I am using Ubuntu 16.04.
cmake_minimum_required(VERSION 3.13)
project(myProject)
set(CMAKE_CXX_STANDARD 11)
add_executable(myProject main.cpp)
find_package(Boost 1.58.0 REQUIRED COMPONENTS system filesystem)
target_link_libraries(myProject ${Boost_LIBRARIES})