Cannot use filesystem::path when compiling with MacPorts LLVM / Clang 15.0.5 on macOS 10.13 or 10.14 [duplicate] - c++

I was trying to out the new filesystem STL library, but for some reason am getting errors. The Clang++7 website indicates that it should support the new filesystem library – indeed clang is running ahead of g++ I believe.
I used some code from another Stack Exchange post, so it should be valid based upon the number of upvotes. This could should go to the specified directory and print all files in that directory. Here is the code.
#include <iostream>
#include <string>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char *argv[])
{
std::string path = "/home/.../Downloads";
for (const auto & entry : fs::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
}
The error messages I am getting are:
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_dir.h:188: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::directory_iterator(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::directory_options, std::error_code*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I made sure to include the experimental/filesystem header instead of just filesystem which removed any red squiggles in Clion. I tried to compile from CLion as well as from the command line. The compilation string I used was:
clang++-7 -Wall -std=c++17 main.cpp -o app
Does anyone have a sense of what is wrong here? In the compile error messages I see the reference to std::experimental::filesystem::v1::__cxx11::.. and I am wondering why this does not say cxx17, but I was not sure if that was the cause of the issue. I explicitly indicated c++17 in the compilation string above.

filesystem is still experimental and requires an extra library.
If you are using libstdc++, link with -lstdc++fs (or target_link_libraries(${PROJECT_NAME} stdc++fs)).
For libc++, use -lc++fs (similar for the CMake command).

Related

Undefined reference error with new filesystem library and clang++7

I was trying to out the new filesystem STL library, but for some reason am getting errors. The Clang++7 website indicates that it should support the new filesystem library – indeed clang is running ahead of g++ I believe.
I used some code from another Stack Exchange post, so it should be valid based upon the number of upvotes. This could should go to the specified directory and print all files in that directory. Here is the code.
#include <iostream>
#include <string>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char *argv[])
{
std::string path = "/home/.../Downloads";
for (const auto & entry : fs::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
}
The error messages I am getting are:
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `main':
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator*() const'
/media/.../clangcpp/filesystem_app/main.cpp:13: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::operator++()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `path<std::__cxx11::basic_string<char>, std::experimental::filesystem::v1::__cxx11::path>':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_path.h:198: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
CMakeFiles/filesystem_app.dir/main.cpp.o: In function `directory_iterator':
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.5.0/../../../../include/c++/5.5.0/experimental/fs_dir.h:188: undefined reference to `std::experimental::filesystem::v1::__cxx11::directory_iterator::directory_iterator(std::experimental::filesystem::v1::__cxx11::path const&, std::experimental::filesystem::v1::directory_options, std::error_code*)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I made sure to include the experimental/filesystem header instead of just filesystem which removed any red squiggles in Clion. I tried to compile from CLion as well as from the command line. The compilation string I used was:
clang++-7 -Wall -std=c++17 main.cpp -o app
Does anyone have a sense of what is wrong here? In the compile error messages I see the reference to std::experimental::filesystem::v1::__cxx11::.. and I am wondering why this does not say cxx17, but I was not sure if that was the cause of the issue. I explicitly indicated c++17 in the compilation string above.
filesystem is still experimental and requires an extra library.
If you are using libstdc++, link with -lstdc++fs (or target_link_libraries(${PROJECT_NAME} stdc++fs)).
For libc++, use -lc++fs (similar for the CMake command).

Undefined references in static OpenCV libraries

I have a project in C++ that uses OpenCV 3.1 and works fine using shared libaries. But now I want to compile it using static libraries (located in a folder within the project directory) because I want to be able to export it (and also edit and recompile if necessary) where OpenCV is not installed.
I have recompiled OpenCV this time setting shared libs to NO:
make -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=NO -DCMAKE_INSTALL_PREFIX=~/Desktop/ocv ..
Then I took my required libraries:
libopencv_core.a libopencv_imgproc.a libopencv_highgui.a
libopencv_video.a libopencv_imgcodecs.a libopencv_videoio.a
and ran g++ a.cpp libopencv_core.a where a.cpp is a sample program to test if everything works:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat a;
printf("hello world\n" );
return 0;
}
My problem is that I can not link the first library (core) because I get lots of undefined references like this:
libopencv_core.a(system.cpp.o): In function `cv::Mutex::Mutex()':
system.cpp:(.text._ZN2cv5MutexC2Ev+0x2c): undefined reference to `pthread_mutexattr_init'
system.cpp:(.text._ZN2cv5MutexC2Ev+0x39): undefined reference to `pthread_mutexattr_settype'
system.cpp:(.text._ZN2cv5MutexC2Ev+0x4c): undefined reference to `pthread_mutexattr_destroy'
libopencv_core.a(system.cpp.o): In function `cv::Mutex::trylock()':
system.cpp:(.text._ZN2cv5Mutex7trylockEv+0x8): undefined reference to `pthread_mutex_trylock'
libopencv_core.a(system.cpp.o): In function `cv::TlsAbstraction::TlsAbstraction()':
system.cpp:(.text._ZN2cv14TlsAbstractionC2Ev+0x9): undefined reference to `pthread_key_create'
libopencv_core.a(system.cpp.o): In function `cv::TlsAbstraction::~TlsAbstraction()':
and so on. I have searched all over and cannot find what's missing. Any help is greatly appreciated.
p.s. G++ and Ubuntu version: g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
You need to link pthread library as well. And pass it as -pthread
g++ a.cpp libopencv_core.a -pthread
You're missing other libraries which contain the required code. There must be a libippicv.a which contains the code for ippicv* functions
g++ a.cpp libopencv_core.a libippicv.a -pthread
It should be somewhere among third_party libs.

Undefined reference to boost::random::random_device constructor and destructor on MinGW-w64 gcc

My OS is Windows 7 64-bit and C++ compiler I'm using is:
g++ (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0
And I installed Boost version 1.60 using:
bootstrap.bat mingw
b2 install target=gcc
Then I tested is it working, using examples from Boost.Random tutorial.
With the first two everything was fine, but the third one gave linker errors about boost::random::random_device. I minimized the code to have only this:
// Compiled with:
// g++ -IC:/Boost/include/boost-1_60
// -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
// main.cpp
#include "boost/random/random_device.hpp"
int main() {
boost::random::random_device rng;
}
And I get the following errors:
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x15):
undefined reference to `boost::random::random_device::random_device()'
C:\Users\Daniel\AppData\Local\Temp\cc5DfdjZ.o:main.cpp:(.text+0x20):
undefined reference to `boost::random::random_device::~random_device()'
collect2.exe: error: ld returned 1 exit status
Here, on SO, I found that someone with similar problem added -lboost_system to flags, but for me it didn't helped.
Does anyone have any idea, why it isn't working? I checked, and I have random_device.hpp header in my Boost folder, with declarations of random_device() and ~random_device() in it.
I found what was wrong - the g++ command syntax, that I wanted to use to compile and link my code.
As I wrote in my question, I do this that way:
g++ -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60 main.cpp
While the correct one is with main.cpp (or any other source code file(s), that we want to include in compiling process) before the -L and -l flags.
For example:
g++ -IC:/Boost/include/boost-1_60 main.cpp -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
or even
g++ main.cpp -IC:/Boost/include/boost-1_60 -LC:/Boost/lib -lboost_random-mgw53-mt-1_60
Hope it will help anyone, who will make such silly mistake too.

OpenCV: Undefined reference to xcb_poll_for_reply

As of late I have been getting the following error whenever I try to compile any program that uses the open cv libraries, I use g++ to compile:
g++ Example.cpp -o Ex `pkg-config opencv --cflags --libs`
No matter the content of the file (I have checked with programs that worked a couple of weeks ago) I always get the following error:
/usr/lib64/libX11.so.6: undefined reference to `xcb_poll_for_reply64'
/usr/lib64/libX11.so.6: undefined reference to `xcb_wait_for_reply64'
Do you have any idea of what might be the cause? (and how to fix it)
An example program that fails to compile:
#include "path/opencv2/highgui/highgui.hpp"
#include "path/opencv/highgui.h"
using namespace cv;
int main (int argc, char * argv[])
{
Mat image = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE) ;
return 0;
}
Add -lxcb to your command line (this will instruct the linker linking w/ the xcb library). Please make sure the 64b version of xcb is in the linker path (you can always put it explicitly via the -L switch)
The error was caused by some changes done to the libX11.so.6, talked with the FE machines support and they fixed it.

How do I link boost to my program?

I downloaded Boost 1.54 and ran bootstrap.bat mingw. Then I tried to run the program below. I am getting the error you see below. I have tried copying my boost folder into the mingw include folder and I have tried linking my file to the boost/stage/lib folder, but I have not been successful. I see a lot of questions similar to this question, but none of them explain how to get the link the boost folder to the file.
Do I have to copy the boost folder to a different directory? Do I have to change my path variable? How can I get the boost library to link to my code?
Code
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace boost::filesystem;
int main()
{
boost::filesystem::directory_iterator iterator(string("."));
for(; iterator != boost::filesystem::directory_iterator(); ++iterator)
{
cout << (iterator->path().filename()) << endl;
}
boost::filesystem::path full_path( boost::filesystem::current_path() );
std::cout << "Current path is : " << full_path << std::endl;
return 0;
}
Error
C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0xa4): undefined reference to `boost::filesystem::path::filename() const'
C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0x244): undefined reference to `boost::system::generic_category()'
C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0x24e): undefined reference to `boost::system::generic_category()'
C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o:playground.cc:(.text+0x258): undefined reference to `boost::system::system_category()'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\212340~1\AppData\Local\Temp\ccZA7umT.o: bad reloc address 0x1b in section `.text$_ZNK5boost6system10error_code7messageEv[__ZNK5boost6system10error_code7messageEv]'
collect2.exe: error: ld returned 1 exit status
[Finished in 21.0s with exit code 1]
Answer
boost::system must be linked explicitly. In contrast to many other parts of boost, boost system is not header-only. Thus, you must make sure to link it when you compile. You have two options for linking.
Reference: http://www.boost.org/doc/libs/1_47_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library
Option 1
Use -lboost_system (or the equivalent file in your /boost_####/stage/lib/ directory). Of course, you also have to set the library path using -L/file/path/to/libraries unless boost system resides in a standard lookup dir.
Example
g++ playground.cc -o playground -L~/boost/stage/lib/ -libboost_filesystem-mgw48-mt-1_54.a
Option 2
Include the full file path to the library at the end of your code.
Example
Run this from the command line. The triple quotes """ are necessary only for paths that contain spaces.
g++ playground.cc -o playground """C:\My Programs\boost_1_54_0\stage\lib\libboost_filesystem-mgw48-mt-1_54.a""" """C:\My Programs\boost_1_54_0\stage\lib\libboost_system-mgw48-mt-1_54.a"""
Note: For a list of files that are not header-only, see http://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#header-only-libraries (Same link as in the first paragraph on "header-only").