How to use boost when installed via homebrew? - c++

This might be a very basic question, but this is my first time with such an issue. I am using an M1 macOS Big Sur. I am trying to run boost libraries for my program. I have installed boost on my device using arch -arm64 brew install boost.
When I try to compile the following program:
#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;
}
with
$ g++ --version
Apple clang version 12.0.5 (clang-1205.0.22.11)
Target: arm64-apple-darwin20.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
I get the following error:
boost.cpp:1:10: fatal error: 'boost/multiprecision/cpp_int.hpp' file not found
#include <boost/multiprecision/cpp_int.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
This is what I see when I go into /opt/homebrew/lib:
cmake libboost_log-mt.dylib libboost_serialization.a libgettextsrc-0.21.dylib
gcc libboost_log.a libboost_serialization.dylib libgettextsrc.dylib
gettext libboost_log.dylib libboost_stacktrace_addr2line-mt.a libgmp.10.dylib
libasprintf.0.dylib libboost_log_setup-mt.a libboost_stacktrace_addr2line-mt.dylib libgmp.a
libasprintf.a libboost_log_setup-mt.dylib libboost_stacktrace_addr2line.a libgmp.dylib
libasprintf.dylib libboost_log_setup.a libboost_stacktrace_addr2line.dylib libgmpxx.4.dylib
libboost_atomic-mt.a libboost_log_setup.dylib libboost_stacktrace_basic-mt.a libgmpxx.a
libboost_atomic-mt.dylib libboost_math_c99-mt.a libboost_stacktrace_basic-mt.dylib libgmpxx.dylib
...
When I cd into cmake, I see that there are a bunch of Boost_...-prefixed files.
It looks like I have boost on my computer, but how do I use it in my program and compile it with g++?

I figured out that the homebrew was adding files to /opt/homebrew/Cellar/boost/1.79.0_1/include/boost. Once the location was identified, I simply compiled by code with flag
g++ -I/opt/homebrew/Cellar/boost/1.79.0_1/include/boost ...
And made sure my include statements referenced the appropriate .hpp files.

Related

libsndfile Emscripten environment

I am running just a little code using libsndfile, in the emscripten environment
#include <iostream>
#include <sndfile.h>
int main()
{
SF_INFO info;
const char * path = "~/data/somefile.wav";
SNDFILE* sf = sf_open(path,SFM_READ, &info);
if(sf == NULL)
{
std::cout<< sf_strerror(sf) << std::endl;
return 1;
}
std::cout<<info.samplerate<<std::endl;
std::cout<<"Hello world" << std::endl;
}
So ideally if I run this with normal cmake (Apple Clang compiler) everything works fine, the samplerate and hello world are printed, but when I run this with emcmake cmake (em++ compiler) and run the compiled node main.js file it says System error: no such file or directory. Who can help me with this? Who has experienced such thing?
So I figured it out.
The problem is that Emscripten has its virtual file environment. So if you want this file to be uploaded and later be seen in compiled .js file, you need to add compile flag --preload-file <FILE_PATH> , after that the file with given path will be recognized by emscripten environment.

Compiling & building CPP using LLVM (Clang) on Windows10

I installed LLVM on a clear Windows 10 machine, without anything else. I installed the ARMCompiler6.14.1. I have a simple cpp for testing:
#include <stdio.h>
#include <iostream>
#include <vector>
int main(void) {
int counter = 0;
counter++;
printf("counter: %d", counter);
std::cout << std::endl;
printf("c++14 output");
std::cout << std::endl;
std::vector<int> vect{1, 2, 3, 4, 5};
for (auto & el : vect)
std::cout << "-" << el << std::endl;
return counter;
}
When I compile this with following line I get errors:
"C:\Program Files\LLVM\bin\clang++.exe" -MD -x c++ "C:\Test\test.cpp" -target armv7a-none-eabi "-IC:\Program Files\ARMCompiler.6.14.1\include"
The error is:
test.cpp 2:10:fatal error: 'iostream' file not found
When I compile this with following line I get an windows executable which runs:
"C:\Program Files\LLVM\bin\clang++.exe" -MD -x c++ "C:\Test\test.cpp"
Since I checked that in the LLVM\bin is anything from the developer.arm.com package (ARMcompiler 6.14.1), I think that I should get an a.out for ARM, but I do not. When I checked the generated object file with:
"C:\Program Files\LLVM\bin\llvm-objdump.exe" -f test.o
I get following:
test.o file format elf32-littlearm
arhitecture: arm
start adress: 0x00000000
which means the object file is correctly generated for ARM, but I did not get an executable a.out for ARM.
What am I missing? How should I compile a simple test program to get it cross-compiled for ARM? Should I get the binutil from GCC for the Clang to create ARM executables?

C++ CodeBlocks upgrade to C++17 can't use std::filesystem

I need to be able to list the files in a directory, and so I'm trying to upgrade my C++ version in CodeBlocks to C++ 17 so i can use filesystem. To do this I followed the steps outlined at http://candcplusplus.com/enable-c17-in-code-blocks-mingw-gcc-for-all-version-with-pictures#:~:text=Enabling%20the%20C%2B%2B17,Create%20a%20project.
I didnt have to change much, CodeBlocks 20.03 and MinGW 8.1.0 are already installed. MinGW is already in my path from when I built wxWidgets. The Settings->Compiler...->Toolchain executables tab I didnt have to make any changes to, and appears in CodeBlocks as:
I also checked the box to use C++ 17 in compiler settings like so
I ran the test program on the website with the instructions and got "True!".
However when I change the basic test program to this, to try and use filesystem to read files in a directory, I get an error:
#include <iostream>
#include <filesystem>
using namespace std;
int main()
{
const int i=90;
if constexpr (i) //'if constexpr' is part of C++17
{
cout << "True!";
}
else
{
cout<<"False" ;
}
std::string path = "../MagicProgCPP/files/debug images/";
for (const auto & entry : filesystem::directory_iterator(path))
{
cout << entry.path() << std::endl;
}
cin.get();
return 0;
}
The program stops building, opens the file fs_path.h and stops on this line:
#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
if (__p.is_absolute()
|| (__p.has_root_name() && __p.root_name() != root_name())) <----- ******STOPS HERE
operator=(__p);
else
{
string_type __pathname;
if (__p.has_root_directory())
__pathname = root_name().native();
else if (has_filename() || (!has_root_directory() && is_absolute()))
__pathname = _M_pathname + preferred_separator;
__pathname += __p.relative_path().native(); // XXX is this right?
_M_pathname.swap(__pathname);
_M_split_cmpts();
}
#else
// Much simpler, as any path with root-name or root-dir is absolute.
if (__p.is_absolute())
operator=(__p);
else
{
if (has_filename() || (_M_type == _Type::_Root_name))
_M_pathname += preferred_separator;
_M_pathname += __p.native();
_M_split_cmpts();
}
#endif
return *this;
}
I get this error in the build log:
C:\Program Files\CodeBlocks\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\bits\fs_path.h|237|error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')|
I'm prety confident the path exists as I entered it and there's files in it. The build log message suggests maybe I'm not using C++17? But when I click build, this is the line the program uses to build:
g++.exe -Wall -fexceptions -g -Wall -std=c++17 -c E:\testc17\main.cpp -o obj\Debug\main.o
What am I doing wrong? Thanks
The bug 78870 was fixed since 2018-07.
You should add to project options -> linker settings -> link libraries the following library: stdc++fs.
I tried to compile your code with MinGW gcc 8.1.0 (via CodeBlocks) and everything works well (clearly with another path, since I don't have the same directories as you).
You could also add a check on the existence of the search directory like this:
namespace fs = std::filesystem;
std::string mypath { "../MyDir" };
if(fs::exists(mypath))
{
for(const auto & entry : fs::directory_iterator(path))
{
cout << entry.path() << std::endl;
}
}
It appears that this exact problem is a known bug in mingw 8.1. The bug report is here: https://sourceforge.net/p/mingw-w64/bugs/737/
and has the error in the same location:
operator != is declared and defined in line 550, but referenced in line 237.
The problem is triggered by operator/= in line 233:
path& operator/=(const path& __p)
{
#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
if (__p.is_absolute()
|| (__p.has_root_name() && __p.root_name() != root_name()))
operator=(__p);
else
{
string_type __pathname;
if (__p.has_root_directory())
__pathname = root_name().native();
else if (has_filename() || (!has_root_directory() && is_absolute()))
__pathname = _M_pathname + preferred_separator;
__pathname += __p.relative_path().native(); // XXX is this right?
_M_pathname.swap(__pathname);
_M_split_cmpts();
}
The bug report said this was fixed in master meaning you need to install a version of mingw with the fix applied. I believe the best method is to upgrade mingw to a version greater than 8.1
user4581301 commented above in the main question that the following link has instructions on how to get a mingw install: How to install MinGW-w64 and MSYS2?

Cannot inspect a std::string variable in lldb

When I try to inspect std::string variable using LLDB, I get "error: summary string parsing error".
#include <iostream>
#include <string>
int main() {
std::string a{"123"};
std::cout << a << std::endl;
return 0;
}
Process 4492 stopped
* thread #1, name = 'main', stop reason = breakpoint 1.1
frame #0: 0x00005555555551e9 main`main at main.cpp:6:1
3
4 int main() {
5 std::string a{"123"};
-> 6 std::cout << a << std::endl;
7 return 0;
8 }
(lldb) v a
(std::string) a = error: summary string parsing error
Additional information:
$ clang++ --version
clang version 8.0.1 (tags/RELEASE_801/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ lldb --version
lldb version 8.0.1
uname -s -r -m -o
Linux 5.3.5-arch1-1-ARCH x86_64 GNU/Linux
Try recompiling your source code with the -fstandalone-debug flag. I had this same problem when I was using lldb today and when I tried accessing the string character-by-character it threw an error that recommended compiling with this flag. After I had recompiled my binary files lldb handled strings just fine.
Note: I'm unsure if this flag works with g++, but I'm assuming that you compile with clang++ if you're using lldb.

How to compile CGAL programs on Mac?

OS X El Capitan 10.11.2
Installed CGAL library via macports (version 2.3.4)
I have a file (/Users/Arseniy/Desktop/vec.cpp):
#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
int main()
{
Point_2 points[5] = { Point_2(0,0), Point_2(10,0), Point_2(10,10), Point_2(6,5), Point_2(4,1) };
Point_2 result[5];
Point_2 *ptr = CGAL::convex_hull_2( points, points+5, result );
std::cout << ptr - result << " points on the convex hull:" << std::endl;
for(int i = 0; i < ptr - result; i++){
std::cout << result[i] << std::endl;
}
return 0;
}
Also i found headers of CGAL library (/usr/local/include/CGAL).
And when i tried to compile it via g++ in terminal (g++ -0 vec vec.cpp -lCGAL -I/usr/local/include/CGAL) I see the error:
"ld: library not found for -lCGAL
clang: error: linker command failed with exit code 1 (use -v to see invocation).
I'm pretty new to gcc. This problem may seem easy to you but I am here to learn.
Thank you for your attention and your help!
I Think Pods isn't being included in the build.
Can you check the pods root path in xcode please? To do this, click "pods" > build settings > and where you see "PODS_ROOT" double click that, which show something like
${SRCROOT}/../Pods
If everything is fine, the issue will be pods isn't being installed, run pods install ( pod install)
For further reference use this link
http://www.binpress.com/tutorial/cocoapods-dependancy-management-for-xcode/139
Sorry If this Doesn't Solves your problem.
You need to provide the linker the path to the CGAL library using the -L option. You are also probably missing the -frounding-math flag.
CGAL recommends to use cmake.