How to install and run a program with openmp c/c++? - c++

I need to install OpenMP on my Mac and use it in CLion. I re-installed gcc, confirmed I have it, /usr/bin/local/gcc-7. Do not understand if this comes with OpenMP or if I need to install something extra.
CMakeLists.txt
cmake_minimum_required(VERSION 3.9)
project(lab_3)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
add_executable(lab_3 main.cpp)
main.cpp
#include <iostream>
#include <omp.h>
int main() {
#pragma omp parallel for
for (int i=0; i<10; i++) {
std::cout << "This is thread #" << omp_get_thread_num() << std::endl;
}
return 0;
}
Error message:
clang: error: unsupported option '-fopenmp'

Related

QtCreator with Intel OneAPI SYCL

I started my study with OneAPI SYCL but I normally use QtCreator as my IDE. I did a HelloSYCL project with CMake and works fine in the terminal and in the VSCode with OneAPI Extension as well, but didn't work in the QtCreator.
Every time I want to use SYCL I need to start ONEAPI environment with ". /opt/intel/oneapi/setvars.sh", but I don't know how to do it with QtCreator
Here is the way I'm compile
mkdir build; cd build
cmake -DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx -G Ninja -S .. -DCMAKE_PREFIX_PATH="/opt/intel/oneapi/compiler/latest/linux/cmake/SYCL/" -DSYCL_INCLUDE_DIR=/opt/intel/oneapi/compiler/latest/linux/include/sycl -DSYCL_LIBRARY_DIR=/opt/intel/oneapi/compiler/latest/linux/lib
cmake_minimum_required(VERSION 3.22)
project(testSYCL LANGUAGES CXX)
if(UNIX)
set(CMAKE_C_COMPILER icx)
set(CMAKE_CXX_COMPILER dpcpp)
endif(UNIX)
list(APPEND CMAKE_MODULE_PATH "/opt/intel/oneapi/compiler/2021.4.0/linux/")
list(APPEND CMAKE_MODULE_PATH "/opt/intel/oneapi/compiler/2021.4.0/linux/cmake/SYCL/")
find_package(IntelDPCPP REQUIRED)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Release")
set(CMAKE_BUILD_TYPE "Release" CACHE PATH "Build Type" FORCE)
endif()
add_executable(testSYCL main.cpp)
#include <iostream>
#include <iomanip>
#include <CL/sycl.hpp>
int main()
{
for ( const auto& plataform : sycl::platform::get_platforms() ) {
std::cout << "=========================================================\n";
std::cout << std::setw(25);
std::cout << plataform.get_info<sycl::info::platform::name>() << "\n";
std::cout << plataform.get_info<sycl::info::platform::vendor>() << "\n";
std::cout << "Plataform: " << plataform.get_info<sycl::info::platform::version>() << "\n";
for ( const auto& device : plataform.get_devices() ) {
std::cout << "Devices\n";
std::cout << "Name: " << device.get_info<sycl::info::device::name>() << "\n";
std::cout << "Max Compute Units: " << device.get_info<sycl::info::device::max_compute_units>() << "\n";
std::cout << "Max Work Group Size: " << device.get_info<sycl::info::device::max_work_group_size>() << "\n";
std::cout << "Max Clock Frequency: " << device.get_info<sycl::info::device::max_clock_frequency>() << " MHz \n";
}
}
}
QtCreator CMake Error
QtCreator Custom Compiler added
The answer depends on the contents of setvars.sh. I assume this is a simple script setting environment variables.
One way to mimic it in QtCreator is to define a custom kit.
Go to Tools/Options/Kits. Highlight any kit you use and press "clone". Then, add to it manually the necessary environment variables in the field "Environment". Use this kit for your SYCL projects. This will work if your problem is caused by a compiler error.
If the problem occurs while running a program inside QtCreator, look at build/running options:
There you'll easily find options for changing the runtime or compile-time environment

CMake: Bundling a Console Application in MacOS c++

I am building a MacOS c++ application in CMake that is a terminal program with a few associated libraries. I would like bundle the executable because customers are familiar with that. However, then opening the bundled .app the terminal will not open and the program disappears immediately even if I request input.
For example, the application main.cpp
#include <iostream>
int main()
{
int x;
std::cout << "Hello World!\n";
std::cin >> x;
std::cout << "You said " << x << "\n";
return 0;
}
can be made up with CMakeLists.txt file:
cmake_minimum_required(VERSION 3.4)
project(hello_world)
SET(CURRENT_TARGET hello)
add_executable(${CURRENT_TARGET} MACOSX_BUNDLE main.cpp)
But if I double-click the hello.app file it creates, it closes immediately. Is there a way to fix this?

Can not find cout uint_32 and other things after set the project to be 32-bit

Here is my code, it correct without CMAKE_CXX_FLAGS "-m32"
#include <iostream>
using namespace std;
int main() {
void *startPos;
cout << startPos << endl;
return 0;
}
It says Use of undeclared identifier 'cout'
Here is my Cmakelist
cmake_minimum_required(VERSION 3.16)
project(test)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "-m32")
include_directories(${PROJECT_SOURCE_DIR}/include)
add_executable(test src/test.cpp)
I'm using wsl2 ubuntu20.04
gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)

How to link boost libraries in VScode using vcpkg & cmake?

I am trying to use boost libraries in VSCode (using Cmake & vcpkg) but can't seem to figure out what's wrong. I installed Boost in vcpkg.
Here are the error messages.
main.cpp
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
using namespace std;
int128_t boost_product(long long A, long long B)
{
int128_t ans = (int128_t) A * B;
return ans;
}
int main()
{
long long first = 98745636214564698;
long long second=7459874565236544789;
cout << "Product of "<< first << " * "
<< second << " = \n"
<< boost_product(first,second) ;
return 0;
}
setting.json
{
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools",
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "C:/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
}
CmakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(demo VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(demo main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Errors:
main.cpp:1:10: fatal error: boost/multiprecision/cpp_int.hpp: No such file or directory
1 | #include <boost/multiprecision/cpp_int.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Maybe I am missing something. Please help.Thank you!

Include Library in my C++ binary - Cmake

I am using the poppler-cpp library in my project.
It build & start it correctly, but I need to have popple installed in my system.
What is the way to import (include) the poppler library in my project, the aim is to execute it on another linux system which has not poppler lib installed, is it possible ?
My current cmakeList.txt file:
cmake_minimum_required(VERSION 3.5)
project(poppler_test)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/extra-cmake-modules/find-modules")
find_package(Poppler REQUIRED)
include(CMakeToolsHelpers OPTIONAL)
include_directories(${Poppler_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
ADD_LIBRARY(poppler STATIC IMPORTED)
add_dependencies( poppler poppler_test )
add_executable(poppler_test ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${Poppler_LIBRARIES})
My main.cpp Programm
#include <algorithm>
#include <cpp/poppler-document.h>
#include <cpp/poppler-page.h>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char* argv[]) {
auto input = "/Home/Dev/poppler-test/test.pdf";
shared_ptr<poppler::document> doc{poppler::document::load_from_file(input)};
const int pagesNbr = doc->pages();
cout << "page count: " << pagesNbr << endl;
vector<string> infoKeys{doc->info_keys()};
for_each(infoKeys.begin(), infoKeys.end(),
[doc](string infoKey) { cout << doc->info_key(infoKey).to_latin1() << endl; });
for (int i = 0; i < pagesNbr; ++i) {
shared_ptr<poppler::page> currentPage{doc->create_page(i)};
if (currentPage == nullptr) {
auto byteArrayResult = currentPage->text().to_utf8();
string result{byteArrayResult.begin(), byteArrayResult.end()};
cout << result << endl;
}
}
}