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?
Related
I want to use pacman to install OpenGL but I get an error compiling.
I want to link with -lglut -lGLU -lGL but what packages are those? Is there a way to find out?
#include <iostream>
using namespace std;
#include <SDL2\SDL.h>
#include <GL\glu.h>
int main(int arc, char *argv[]) {
cout << "Hello world" << endl;
glShadeModel(GL_SMOOTH);
return 0;
}
I run (obviously I get an error saying the lib is not found if using -lGLU)
g++ *.cpp
This is the error
C:\Users\User\AppData\Local\Temp\ccztMdnY.o:main.cpp:(.text+0x39): undefined reference to `__imp_glShadeModel'
F:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.3.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
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 am new at OpenCv. I am using Eclipse C/C++. When i try to run this sample code i faced with these errors. What should i do to solve this problem? Is there any problem at configurating ?
#using namespace std;
#using namespace cv;
int main( int argc, const char** argv )
{
Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED);
if (img.empty()) //check whether the image is loaded or not
{
cout << "Error : Image cannot be loaded..!!" << endl;
//system("pause"); //wait for a key press
return -1;
}
namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); /
waitKey(0); //wait infinite time for a keypress
destroyWindow("MyWindow"); //destroy the window with the name, "MyWindow"
return 0;
}
16:11:28 **** Incremental Build of configuration Debug for project OpenCv ****
Info: Internal Builder is used for build
g++ "-IC:\\opencv\\build\\include" -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\OpenCv.o" "..\\src\\OpenCv.cpp"
g++ "-LC:\\opencv\\build\\x86\\vc12\\lib" -o OpenCv.exe "src\\OpenCv.o" -lopencv_calib3d249 -lopencv_contrib249 -lopencv_core249 -lopencv_features2d249 -lopencv_flann249 -lopencv_gpu249 -lopencv_highgui249 -lopencv_imgproc249 -lopencv_legacy249 -lopencv_ml249 -lopencv_nonfree249 -lopencv_objdetect249 -lopencv_ocl249 -lopencv_photo249 -lopencv_stitching249 -lopencv_superres249 -lopencv_ts249 -lopencv_video249 -lopencv_videostab249
src\OpenCv.o: In function `main':
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:10: undefined reference to `cv::imread(std::string const&, int)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:19: undefined reference to `cv::namedWindow(std::string const&, int)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:20: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:20: undefined reference to `cv::imshow(std::string const&, cv::_InputArray const&)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:22: undefined reference to `cv::waitKey(int)'
C:\Users\ayberk101\workspace\OpenCv\Debug/../src/OpenCv.cpp:24: undefined reference to `cv::destroyWindow(std::string const&)'
src\OpenCv.o: In function `ZN2cv3MatD1Ev':
C:/opencv/build/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
src\OpenCv.o: In function `ZN2cv3Mat7releaseEv':
C:/opencv/build/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2.exe: error: ld returned 1 exit status
problem1: you will have to include opencv / c++ header files to make it work:
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#using namespace cv;
#include <iostream>
#using namespace std;
int main() {
...
then, problem2: you cannot use the vc12 libs with mingw. (it's a different compiler)
there are no more prebuild mingw libs for opencv, so, before doing anything else, you will have to build the opencv libs locally using cmake.
again, do you really need to use mingw / eclipse ? (vs express is still free)
I'm trying to run this code using c++11 std::thread and the SFML library for networking, however I just cant get it to build. I'm pretty sure it has something to do with how I'm setting up the compiler, but I don't know... How can I get this to work? I've been messing about trying to get this to work, going through tutorials and such for hours now, so I have turned to Stack Overflow for answers. I'm using the code::blocks IDE and the x32-4.8.1-posix-dwarf-rev5 compiler on a windows7 64-bit machine.
#include <iostream>
#include <thread>
#include <string>
#include <SFML/System.hpp>
#include <SFML/Network.hpp>
using namespace std;
void doNetwork() {
cout << "Starting..." << endl;
sf::TcpListener listener;
listener.listen(88);
// Wait for a connection
sf::TcpSocket socket;
listener.accept(socket);
cout << "New client connected: " << socket.getRemoteAddress() << endl;
// Receive a message from the client
char buffer[1024];
size_t received = 0;
socket.receive(buffer, sizeof(buffer), received);
cout << "The client said: " << buffer << endl;
// Send an answer
string message = "Welcome, client";
socket.send(message.c_str(), message.size() + 1);
}
int main()
{
thread task(doNetwork);
task.join();
return 0;
}
This is what the build debug log is saying:
-------------- Build: Debug in bang ---------------
Compiling: main.cpp
Linking console executable: bin\Debug\bang.exe
obj\Debug\main.o: In function `Z9doNetworkv':
C:/Users/user/CppProjects/bang/main.cpp:12: undefined reference to `sf::TcpListener::TcpListener()'
C:/Users/user/CppProjects/bang/main.cpp:13: undefined reference to `sf::TcpListener::listen(unsigned short)'
C:/Users/user/CppProjects/bang/main.cpp:16: undefined reference to `sf::TcpSocket::TcpSocket()'
C:/Users/user/CppProjects/bang/main.cpp:17: undefined reference to `sf::TcpListener::accept(sf::TcpSocket&)'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::TcpSocket::getRemoteAddress() const'
C:/Users/user/CppProjects/bang/main.cpp:18: undefined reference to `sf::operator<<(std::ostream&, sf::IpAddress const&)'
C:/Users/user/CppProjects/bang/main.cpp:23: undefined reference to `sf::TcpSocket::receive(void*, unsigned int, unsigned int&)'
C:/Users/user/CppProjects/bang/main.cpp:28: undefined reference to `sf::TcpSocket::send(void const*, unsigned int)'
obj\Debug\main.o: In function `ZN2sf11TcpListenerD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpListener.hpp:43: undefined reference to `sf::Socket::~Socket()'
obj\Debug\main.o: In function `ZN2sf9TcpSocketD1Ev':
C:/Users/user/CppProjects/SFML-2.1/include/SFML/Network/TcpSocket.hpp:46: undefined reference to `sf::Socket::~Socket()'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
10 errors, 0 warnings
The settings I am using with the compiler:
Compiler Flags:
-std=C++0x
Other options:
-std=gnu++0x -U__STRICT_ANSI__
defines:
SFML_STATIC
Linker Settings:
sfml-system-s-d
sfml-network-s-d
Search Directories - Compiler:
C:\Users\user\CppProjects\SFML-2.1\include
C:\Users\user\CppProjects\bang\include
Search Directories - Linker:
C:\Users\user\CppProjects\SFML-2.1\lib\
Toolchain Executables:
-Compilers instalation dir:
C:\Program Files (x86)\mingw-builds\x32-4.8.1-posix-dwarf-rev5\mingw32
-C compiler:
i686-w64-mingw32-gcc-4.8.1.exe
-C++ Compiler:
i686-w64-mingw32-c++.exe
-Linker for dynamic libs:
i686-w64-mingw32-c++.exe
-Linker for static libs:
ar.exe
-Debugger:
gdb.exe
-Resource Compiler:
windres.exe
-Make Program:
mingw32-make.exe
I had a simular problem and how i solved this on a Linux system was: Project / build options
Go to linker settings tab.
there i added: sfml-network
Done.
Hope it helps
I'm trying to use the boost/filesystem library in some code that I am writing. I seem to be having a hard time getting it to compile. I'm running Debian Wheezy, and have boost version 1.49(which is what comes if you install using apt-get). I'm trying to compile an example that is available with the documentation
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
int main(int argc, char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: tut1 path\n";
return 1;
}
std::cout << argv[1] << " " << file_size(argv[1]) << '\n';
return 0;
}
I use the following command:
g++ temp.cc -o temp /usr/lib/libboost_filesystem.a
I get a number of errors such as:
/usr/lib/libboost_filesystem.a(operations.o): In function `boost::filesystem3::detail::dir_itr_close(void*&, void*&)':
(.text+0x4d): undefined reference to `boost::system::system_category()'
/usr/lib/libboost_filesystem.a(operations.o): In function `boost::filesystem3::detail::directory_iterator_increment(boost::filesystem3::directory_iterator&, boost::system::error_code*)':
(.text+0xe3): undefined reference to `boost::system::system_category()'
This is probably some linking error right? Any ideas on how I could solve it?
UPDATE #1:
I tried running it with the -lboost_filesyste and -L /usr/lib. It gives me the following error:
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
You are not linking the library properly. Also, as others mentioned, boost_filesystem needs also boost_system library. Use:
g++ temp.cc -o temp -lboost_system -lboost_filesystem
Command line param -l foo links libfoo.a library. If the static library is not in default library location, use command -L /custom/library/dir. But I believe /usr/lib is automatically taken into consideration by GCC.
Edit
According to your comment below it looks like you are not compiling the file with main() function, or you have a typo in main() name. Make sure that temp.cc contains one and only one of these functions:
int main();
int main(int argc, char** argv);
Of course you do remember that upper/lower case matters. :)
Boost.Filesystem uses things in Boost.System. You have to link against that, too.
The error messages that you are seeing:
/usr/lib/libboost_filesystem.a(operations.o): In function
`boost::filesystem3::detail::dir_itr_close(void*&, void*&)':
(.text+0x4d): undefined reference to `boost::system::system_category()'
that's a reference to Boost.System
Add -lboost_system and you should be good to go (or, at least better off).
Compile with -lboost_filesystem