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").
Related
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).
I am trying to install the FreeImage library in C++ using Dev-Cpp on Windows 10, i put the .h files in the corresponding folder and i added the directories in Dev-cpp, the .DLL file i put it in the folder: C:\Program Files (x86)\Dev-Cpp\MinGW64\bin that it's where i have installed Dev-Cpp,also a .lib file i put it in the next folder: C:\Program Files(x86)\Dev-Cpp\MinGW64\lib that also it's where i have installed Dev-Cpp, i added -l FreeImage to the linker too.
when i compile the code the .h files are found correctly, but Dev-Cpp gives me errors of type Undefined reference to,i understand this is because C++ can't found where it's the functions defined.
Code:
#include <iostream>
#include <FreeImagePlus.h>
int main(){
fipImage img;
img.load("Goten.jpg");
return 0;
}
Errors:
C:\Users\admin\AppData\Local\Temp\ccjljTTu.o Aburrido2.cpp:(.text+0x36): undefined reference to `__imp__ZN8fipImageC1E15FREE_IMAGE_TYPEjjj'
C:\Users\admin\AppData\Local\Temp\ccjljTTu.o Aburrido2.cpp:(.text+0x53): undefined reference to `__imp__ZN8fipImage4loadEPKci'
C:\Users\admin\AppData\Local\Temp\ccjljTTu.o Aburrido2.cpp:(.text+0x68): undefined reference to `__imp__ZN8fipImageD1Ev'
C:\Users\admin\AppData\Local\Temp\ccjljTTu.o Aburrido2.cpp:(.text+0x7f): undefined reference to `__imp__ZN8fipImageD1Ev'
C:\Users\admin\Desktop\Programacion\collect2.exe [Error] ld returned 1 exit status
Linker:
-static-libgcc -lFreeImage
What i need to do? - Sorry for my english.
I would like to be able to connect from my c++ program to a local MySQL instance, but the following minimal file testfile.cpp does not compile and returns undefined references:
#include <mysqlx/xdevapi.h>
using namespace ::mysqlx;
int main()
{
printf("Hello world!\n");
return 0;
}
I suspect to not use the right compile flags. When I use the command
c++ -o test1 -std=c++11 -lmysqlcppconn8 -I /usr/include/mysql-cppconn-8/ testfile.cpp
I am getting the following error messages (translated to English):
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::string::traits<char>::to_str[abi:cxx11](mysqlx::abi2::r0::string const&)":
testfile.cpp:(.text._ZN6mysqlx4abi22r06string6traitsIcE6to_strB5cxx11ERKS2_[_ZN6mysqlx4abi22r06string6traitsIcE6to_strB5cxx11ERKS2_]+0x2e): undefined reference to "mysqlx::abi2::r0::string::Impl::to_utf8[abi:cxx11](mysqlx::abi2::r0::string const&)"
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::DbDoc::DbDoc()":
testfile.cpp:(.text._ZN6mysqlx4abi22r05DbDocC2Ev[_ZN6mysqlx4abi22r05DbDocC5Ev]+0x1b): undefined reference to "vtable for mysqlx::abi2::r0::DbDoc"
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::DbDoc::~DbDoc()":
testfile.cpp:(.text._ZN6mysqlx4abi22r05DbDocD2Ev[_ZN6mysqlx4abi22r05DbDocD5Ev]+0xf): undefined reference to "vtable for mysqlx::abi2::r0::DbDoc"
/tmp/cc02ZbBr.o: In the function "mysqlx::abi2::r0::Value::print(std::ostream&) const":
testfile.cpp:(.text._ZNK6mysqlx4abi22r05Value5printERSo[_ZNK6mysqlx4abi22r05Value5printERSo]+0x88): undefined reference to "mysqlx::abi2::r0::common::Value::print(std::ostream&) const"
/tmp/cc02ZbBr.o:(.data.rel.ro._ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTVN6mysqlx4abi22r05ValueE]+0x18): undefined reference to "typeinfo for mysqlx::abi2::r0::common::Value"
/tmp/cc02ZbBr.o:(.data.rel.ro._ZTCN6mysqlx4abi22r05ValueE0_NS1_6common5ValueE[_ZTVN6mysqlx4abi22r05ValueE]+0x20): undefined reference to "mysqlx::abi2::r0::common::Value::print(std::ostream&) const"
/tmp/cc02ZbBr.o:(.data.rel.ro._ZTIN6mysqlx4abi22r05ValueE[_ZTIN6mysqlx4abi22r05ValueE]+0x28): undefined reference to "typeinfo for mysqlx::abi2::r0::common::Value"
collect2: error: ld returned 1 exit status
The header from this file comes from a sample code on MySQL Connector/C++'s Github.
This question on SO seems relevant but the syntax/directories might be outdated. In any case, I do not know how to adjust the answers given there to my situation and location of libraries. Therefore I am asking for help here.
More information:
I'm running Linux Ubuntu 18.04, MySQL version 8.0.19 and have the following files in /usr/lib/x86_64-linux-gnu/
libmysqlcppconn.so
libmysqlcppconn.so.7.8.0.19
libmysqlcppconn.so.7
but I do not know how to refer to them.
In /usr/include/mysql-cppconn-8/ I have the directories jdbc/, mysql/ and mysqlx/.
I installed the following binary packages using the apt package manager: libmysqlcppconn-dev, libmysqlcppconn7, libmysqlcppconn8-1 and libmysqlcppconn8-2 (which is probably overkill but according to the installation guide one has to install quite a few of these libraries).
which mysql returns /usr/bin/mysql
When you compile source files and link binaries with object files and libraries, the order does matter. Shared libraries providing exported symbols must follow object files and other shared libraries importing these symbols. In your case, the shared library must be placed in the end of the c++ command invitation:
c++ -o test1 -std=c++11 -I /usr/include/mysql-cppconn-8/ testfile.cpp -lmysqlcppconn8
The undefined symbols discovered after compiling testfile.cpp will be imported from the following libmysqlcppconn8.so. Linkers doesn't remember exported symbols from prior libraries. For more information read this nice article: Why does the order in which libraries are linked sometimes cause errors in GCC.
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).
So I'm trying to compile and run a simple boost timer program
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
int main() {
using namespace boost::asio;
io_service io;
deadline_timer t(io, boost::posix_time::seconds(5));
t.wait();
std::cout << "Hello World!" << std::endl;
return 0;
}
The first thing I tried when compiling this program was to do
g++ -I /home/vagrant/boost_1_60_0 main.cpp
which gave me an error of
/tmp/cc8Ytqko.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0xfc): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x108): undefined reference to `boost::system::generic_category()'
main.cpp:(.text+0x114): undefined reference to `boost::system::system_category()'
/tmp/cc8Ytqko.o: In function `boost::system::error_code::error_code()':
main.cpp:(.text._ZN5boost6system10error_codeC2Ev[_ZN5boost6system10error_codeC5Ev]+0x17): undefined reference to `boost::system::system_category()'
/tmp/cc8Ytqko.o: In function `boost::asio::error::get_system_category()':
main.cpp:(.text._ZN5boost4asio5error19get_system_categoryEv[_ZN5boost4asio5error19get_system_categoryEv]+0x5): undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
So then I did some research and it seems I needed to build the boost_system binaries so I went to directory boost was located in and ran
./bootstrap.sh
./b2 --with-system
Then I compiled again
g++ -I /home/vagrant/boost_1_60_0 main.cpp -L/home/vagrant/boost_1_60_0/stage/lib/ -lboost_system
and this didn't give me any error but when I ran the executable
vagrant#vagrant-ubuntu-trusty-64:/vagrant$ ./a.out
./a.out: error while loading shared libraries: libboost_system.so.1.60.0: cannot open shared object file: No such file or directory
Don't really know what I need to do here
liibboost_system.so.1.60.0 cannot be found in the list of directories searched by the dynamic linker. The non-default shared-object location is not stored in the binary by default. The environment variable LD_LIBRARY_PATH can be used to add directories that will be searched before the standard locations:
LD_LIBRARY_PATH=/home/vagrant/boost_1_60_0/stage/lib/ ./a.out
This will only work for the current bash environment, and there are also ways to store the path in the executable so that the environment variable is not needed.