I have following program which recursively traverses all the files inside a directory->
#include "boost/filesystem.hpp"
#include "boost/regex.hpp"
#include "string"
#include <iostream>
using namespace boost;
using namespace std;
int main()
{
filesystem::path current_dir("."); //
boost::regex pattern("a.*"); // list all files starting with a
for (filesystem::recursive_directory_iterator iter(current_dir), end;
iter != end;
++iter)
{
std::string name = iter->path().filename().string();
if (regex_match(name, pattern))
std::cout << iter->path() << "\n";
}
}
I compile this program using command:
g++ directory_iterate.cpp -lboost_filesystem -lboost_regex -lboost_system
and I get following errors:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libboost_regex.so: undefined reference to `std::__detail::_List_node_base::_M_unhook()#GLIBCXX_3.4.15'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libboost_regex.so: undefined reference to `std::overflow_error::~overflow_error()#GLIBCXX_3.4.15'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libboost_regex.so: undefined reference to `std::__detail::_List_node_base::_M_hook(std::__detail::_List_node_base*)#GLIBCXX_3.4.15'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libboost_regex.so: undefined reference to `std::invalid_argument::~invalid_argument()#GLIBCXX_3.4.15'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libboost_filesystem.so: undefined reference to `std::__throw_out_of_range_fmt(char const*, ...)#GLIBCXX_3.4.20'
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../libboost_regex.so: undefined reference to `std::__detail::_List_node_base::_M_transfer(std::__detail::_List_node_base*, std::__detail::_List_node_base*)#GLIBCXX_3.4.15'
Related
I'm making program to read and write json file.
My code:
#include <bits/stdc++.h>
#include <json/json.h>
#include <json/reader.h>
using namespace std;
int main()
{
ifstream file("input.json");
Json::Value value;
Json::Reader reader;
reader.parse(file, value);
cout << value << endl;
return 0;
}
When I run the program, it's return errors:
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x3f): undefined reference to `Json::Value::Value(Json::ValueType)'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x4b): undefined reference to `Json::Reader::Reader()'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x6b): undefined reference to `Json::Reader::parse(std::istream&, Json::Value&, bool)'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0x7e): undefined reference to `Json::operator<<(std::ostream&, Json::Value const&)'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0xaa): undefined reference to `Json::Value::~Value()'
C:\Users\Admin\AppData\Local\Temp\ccpsFhxG.o:filemanager.cpp:(.text+0xdd): undefined reference to `Json::Value::~Value()'
I already try to add param -ljsoncpp but it said C:/Program Files (x86)/Dev-Cpp/MinGW64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -ljsoncpp
It's there any way to fix this problem? I installed jsoncpp from https://github.com/open-source-parsers/jsoncpp.
I fixed this problem. Just need to run file amalgamate.py (Python >= 2.6 required), it will create three files:
dist/jsoncpp.cpp, the source file to be added to your project
dist/json/json.h, the correspondent header file
dist/json/json-forwards.h, which contains forward declarations of JSON types.
Import file jsoncpp.cpp to your program and it works fine.
I am pretty new to c and c++, so please try explain more specific what I should do. The program tries to read files from a directory using multithreads, and store the information in a map so that it can be used later.
I have been looking for similar posts. However, I am not able to figure out.
In https://github.com/kaldi-asr/kaldi/issues/938, it said that "If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro."
The solution for undefined reference to `pthread_cancel' (add "-pthread" flag does not work either.
My code is
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <random>
#include <unistd.h>
#include <cmath>
#include <stdlib.h>
#include <mutex>
#include <sys/wait.h>
#include <filesystem>
#include <string>
#include <pthread.h>
#define time_str(s) (s < 60 ? (to_string(s) + " second(s)") : (s < 3600 ? (to_string((s) / 60) + " minute(s)") : (to_string((s) / 3600) + " hour(s) and " + to_string(((s) % 3600) / 60) + " minute(s)")))
using namespace std;
namespace fs = std::filesystem;
struct MyGenom
{
vector<string> filepaths;
map<string, string> seq;
};
void check_rv(int rv) {
if (rv != 0) {
printf("Error: Value is %d\n", rv);
exit(1);
}
}
struct Reference_Genome {
static long unsigned int idx;
static map <string, string> seq;
static pthread_mutex_t mtxLock;
static vector <string> filepaths;
static void writing(string path) {
}
static void *distribution(void *var) {
}
Reference_Genome(string dir, unsigned int n_threads) {
}
};
int main(int argc, char const *argv[]) {
string dir = "./data/ex_seq";
unsigned int n_threads = 5;
Reference_Genome ref(dir, n_threads);
cout << "chr6: " << ref.seq["chr6"] << endl;
cout << "chr9: " << ref.seq["chr9"] << endl;
cout << "chr13: " << ref.seq["chr13"] << endl;
}
The gcc version is "Thread model: posix
gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)".
The error is
testSeq.cpp:97: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:98: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:99: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::writing(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
/testSeq.cpp:46: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:48: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::distribution(void*)':
testSeq.cpp:55: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:55: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:56: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:57: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:57: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:58: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:58: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:59: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::Reference_Genome(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int)':
testSeq.cpp:68: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:70: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:72: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:85: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:88: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
collect2: error: ld returned 1 exit status
When you declare static variables inside a class, you must also declare it exactly once outside of the class. In this case, you could put this in the bottom of your C++ file or in between the main() function and the class Reference_Genome definition:
long unsigned int Reference_Genome::idx;
map <string, string> Reference_Genome::seq;
pthread_mutex_t Reference_Genome::mtxLock;
vector <string> Reference_Genome::filepaths;
The idea is that you can put the class definition inside a header file, to be included in multiple different compilation units, but the static variables are only defined once, in one .cpp file of your choosing.
I'm trying to compile a DLIB example: http://dlib.net/image_ex.cpp.html
and I copied it word-for-word:
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
#include <dlib/image_transforms.h>
#include <fstream>
using namespace std;
using namespace dlib;
// ----------------------------------------------------------------------------
int main(int argc, char** argv)
{
try
{
// make sure the user entered an argument to this program
if (argc != 2)
{
cout << "error, you have to enter a BMP file as an argument to this program" << endl;
return 1;
}
And I used this command:
g++ dlib1.cpp -o dlib1 -std=c++11 -O3 -I ~/Packages/dlib/ -lpthread -lX11 -ljpeg -lpng -DDLIB_JPEG_SUPPORT -DDLIB_PNG_SUPPORT
And despite that command working fine months ago: [Can't include the JPEG_SUPPORT headers in a dlib cpp file ] this time, it gave me hundreds of errors:
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x7e): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x94): undefined reference to `dlib::png_loader::is_gray() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xaa): undefined reference to `dlib::png_loader::is_graya() const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0xd0): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x176): undefined reference to `dlib::png_loader::get_row(unsigned int) const'
dlibTest-0.cpp:(.text._ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_[_ZNK4dlib10png_loader9get_imageINS_7array2dINS_9rgb_pixelENS_33memory_manager_stateless_kernel_1IcEEEEEEvRT_]+0x1ce): undefined reference to `dlib::png_loader::is_graya() const'
What exactly is going wrong here?
Some of the samples need a command line such as ...
g++ -std=c++11 -O3 -I.. ../dlib/all/source.cpp -lpthread -lX11 example_program_name.cpp
I guess source.cpp is a "library as cpp code" link : dlib compile
I'm trying to compile my C++ code using Magick++ library to manipulate images in a distributed way using openMPI and I get some errors when I try to compile it.
This is my code:
#include "mpi.h"
#include <stdio.h>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int argc, char **argv){
int rank, numtask;
InitializeMagick(*argv);
Image image;
try {
// Read a file into image object
image.read( "test_image.jpg" );
image.type( GrayscaleType );
Blob blob;
image.magick( "JPEG" ); // Set JPEG output format
image.write( &blob );
}
catch( Exception &error_ ){
cout << "Caught exception: " << error_.what() << endl;
return 1;
}
//Now in the "distributed enviroment" I just print an hello world to test it.
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtask);
cout<<"HelloWorld\n";
MPI_Finalize();
}
this is the command that I type on the shell
mpiCC openmpi_project.cc -o openmpi_project
and this is the output of the shell
openmpi_project.cc:(.text+0x1d): undefined reference to
"Magick::InitializeMagick(char const*)"
openmpi_project.cc:(.text+0x29): undefined reference to
"Magick::Image::Image()"
openmpi_project.cc:(.text+0x5d): undefined reference to
"Magick::Image::read(std::string const&)"
openmpi_project.cc:(.text+0x86): undefined reference to
"Magick::Image::type(MagickCore::ImageType)"
openmpi_project.cc:(.text+0x92): rundefined reference to
"Magick::Blob::Blob()"
openmpi_project.cc:(.text+0xc6): undefined reference to
"Magick::Image::magick(std::string const&)"
openmpi_project.cc:(.text+0xf1): undefined reference to
"Magick::Image::write(Magick::Blob*)"
openmpi_project.cc:(.text+0xfd): undefined reference to
"Magick::Blob::~Blob()"
openmpi_project.cc:(.text+0x158): undefined reference to
"Magick::Image::~Image()"
openmpi_project.cc:(.text+0x1d3): undefined reference to
"Magick::Blob::~Blob()"
openmpi_project.cc:(.text+0x261): undefined reference to
"Magick::Image::~Image()"
/tmp/ccqFzUdy.o:(.gcc_except_table+0x58): undefined reference to
"typeinfo for Magick::Exception"
ImageMagick ships with config utilities. For Magick++ this utility is Magick++-config. See the Usage sub-section under the API docs.
LDFLAGS=$(Magick++-config --ldflags)
CXXFLAGS=$(Magick++-config --cxxflags)
$(CC) $CXXFLAGS openmpi_project.cc $LDFLAGS -o openmpi_project
Jump over to the MPI compiling/linking docs, and integrate Magick++'s additional flags to mpiCC
LDFLAGS=$(Magick++-config --ldflags)
CXXFLAGS=$(Magick++-config --cxxflags)
mpiCC --with-wrapper-cxxflags=$CXXFLAGS openmpi_project.cc \
--with-wrapper-ldflags=$LDFLAGS -o openmpi_project
I need to iterate through files in a folder using C++ and I am using Boost filesystem library for that purpose.
I am trying to run the tutorial code in http://www.boost.org/doc/libs/1_54_0/libs/filesystem/doc/tutorial.html
After installing boost, running bootstrap.sh to build all libraries followed by ./b2, I am able to run some simple code. But when I try to compile the following code from the tutorial
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if(argc < 2)
{
std::cout << "Usage: a.out path\n";
return 1;
}
path my_path(argv[1]);
std::cout << argv[1] << " " << file_size(my_path) << '\n';
return 0;
}
using
g++ f1.cpp -lboost_filesystem -lboost_system
I get the following error:-
/tmp/cc3IJurw.o: In function `boost::filesystem::file_size(boost::filesystem::path const&)':
f1.cpp:(.text._ZN5boost10filesystem9file_sizeERKNS0_4pathE[boost::filesystem::file_size(boost::filesystem::path const&)]+0x19): undefined reference to `boost::filesystem::detail::file_size(boost::filesystem::path const&, boost::system::error_code*)'
/tmp/cc3IJurw.o: In function `boost::filesystem::path::path<char*>(char* const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<char*>::type>, void>::type*)':
f1.cpp:(.text._ZN5boost10filesystem4pathC2IPcEERKT_PNS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIS4_E4typeEEEvE4typeE[_ZN5boost10filesystem4pathC5IPcEERKT_PNS_9enable_ifINS0_11path_traits11is_pathableINS_5decayIS4_E4typeEEEvE4typeE]+0x22): undefined reference to `boost::filesystem::path::codecvt()'
collect2: ld returned 1 exit status
Does anyone know a fix to this?