Undefined reference to std::*something* when linking library staticly - c++

I've compiled some of my code into a static library. Everything from this library begins with Glow or GLOWE prefix. At the moment, I'm testing the library in Linux (Ubuntu 14.04). I made a simple program to check if I did everything correctly.
#include <GlowSystem/Package.h>
int main(void)
{
GLOWE::Package package;
return 0;
}
GLOWE::Package is a class. It uses libzip and zlib (and standard c++ files eg. string). I link both libzip and zlib. When I try to compile, it fails with some linking errors.
Build log (at pastebin)
I thought that these errors are caused by too old libstdc++, but this code compiles:
#include <string>
using namespace std;
int main(void)
{
string a;
a.resize(5000);
return 0;
}
I'm at my wits' end and I have no idea what to do. I will appreciate any help.

It looks like your linker options are incorrect:
../GlowE/GlowEngine/bin/Debug/libGlowEngine.a /usr/lib/x86_64-linux-gnu/libzip.a /usr/lib/x86_64-linux-gnu/libz.a
Try:
-l../GlowE/GlowEngine/bin/Debug/GlowEngine -l/usr/lib/x86_64-linux-gnu/zip -l/usr/lib/x86_64-linux-gnu/z

Related

Trying to use Boost library on Codeblocks gives an undefined reference

I'm trying to use the boost library on CodeBlocks, but I'm new to it and I can't seem to be able to link it properly.
The boost folder(version 1.70) is in the same folder of my main.cpp, and the library I'm trying to access is libboost_filesystem-mgw92-mt-x64-1_70.a;
Here is my code:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path l_path("C:\\Hello.txt");
if(boost::filesystem::exists(l_path))
{
std::cout<<"exists!"<<std::endl;
}
else
{
std::cout<<"no";
}
return 0;
}
And some screenshots of my settings and of the error
Thank you!
Undefined reference to _Unwind_Resume suggests that you build Boost with different compiler than your project or you choose different type of exception handling.
Check if you're using the same compiler in both cases.
It might be also caused by building your project using gcc instead of g++. You should check that as well. In this case switch to g++ or explicitly link against libstdc++, by adding -lstdc++ to compiler flags.

Using Boost library in XCode

not particularly good with this kind of stuff I have managed to install the Boost C++ library and this basic program works with the g++ compiler and linking to the right .dylib files.
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost;
int main()
{
filesystem::path path1("/Users/username/Documents/testfile.txt");
filesystem::remove(path1);
std::cout << "It worked" << std::endl;
return 0;
}
Now I would like to do the same, just using Xcode to build and run the program, but it doesn't work. I have done everything in this tutorial:
http://buddypanda.com/?p=10
i.e. include header and library search paths, as well as linked the .dylib files of filesystem and system under the 'Build Phases' tab.
The Build succeeds but then the program crashed with an error of EXC_BAD_ACCESS.
I can maybe provide more specifics on what goes wrong if you tell me where to look.
Thanks!
EDIT1: I am using g++-4.9 and Boost-1.58 and Xcode 6.4

undefined reference to boost::gregorian::greg_month::as_short_string() const

This was asked several times however I don't know what I'm doing wrong. I'm trying to get the current date subtracted by 7. Here's the Main:
#include <iostream>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/date_formatting.hpp>
#include <boost/date_time/gregorian/greg_month.hpp>
using namespace std;
using namespace boost::gregorian;
int main(int argc, char **argv) {
time_t rawtime;
struct tm *timeinfo;
time (&rawtime);
timeinfo = localtime (&rawtime);
date cdate(timeinfo->tm_year+1900, timeinfo->tm_mon+1, timeinfo->tm_mday);
cdate += date_duration(-7);
string date = to_iso_string(cdate);
cout << date << endl;
return 0;
}
When I try to compile it I get the following error.
E:/include/boost/date_time/date_formatting.hpp:44: undefined reference to `boost::gregorian::greg_month::as_short_string() const'
E:/include/boost/date_time/date_formatting.hpp:49: undefined reference to `boost::gregorian::greg_month::as_long_string() const'
Can anyone help? I thought I included the neccessary files..
Boost date_time is not a header-only library. Please build the library and then add it. Simple in gcc:
gcc myapp.cpp -omyapp -lboost_date_time
(Be careful! This library sneakily appears to work as a header-only library at optimization levels -O2 and higher, due to inlining; but it will fail to link when you use lower optimization levels where the compiler's inliner isn't as aggressive.)
I think the compiler is complaining about the inclusion of boost lib.
In order to use boost::gregorian(boost::date_time), you need to use
bjam to build boost library and then link it against the FileSystem lib.
The reference of boost see click here.
EDIT: According to what you got above, the problem is that the library can't be found, mingw seems like don't know where it is. A re-installation of mingw maybe required or you can try to specify the specific path of the library.
Good luck!
you should add the link lib named
libboost_date_time-mgw46-d-1_54.dll.a
(my path D:\My Documents\Downloads\boost_1_54_0\bin.v2\libs\date_time\build\gcc-mingw-4.6.2\debug\libboost_date_time-mgw46-d-1_54.dll.a) to the compiler's path
Good luck
The reason of the linking issue is the class grep_month part of implementation is at other cpp file located in file boost_xxx_xx_x\libs\date_time\src\gregorian\greg_month.cpp. So this should be built into a static library or directly built into your target.
The other reason of why the "Release" mode with option "O2" building could pass ok, it should be caused by the final codes has not called the gregorian::greg_month related codes, and the complier ignore linking the unused function into the target, so the building is sneakily passed.
So the CyberGuy's comments in the stackoverflow website about the argument of inlining should just be a guess.

Error with zip_open from libzip

I am learning C++, and I decided to make a little program that zip/unzip files to train me.
I downloaded libzip and zlib and linked them to my compiler (MinGW with Code::Blocks on Windows). So I tried to open my zip file with zip_open() and got an error :
undefined reference to _imp__zip_open
Here is the code:
#include <zip.h>
#include <zlib.h>
int main()
{
int error(0);
zip *foo = zip_open("foo.zip", 0, &error);
return 0;
}
I don't know where this is coming from and I would really like some help, because I don't find anything on Google (surely cause the problem is simple).
Thanks in advance!
It looks like you haven't linked to libzip. Make sure you are infact linking to it, and that the path to the lib is in your link path.
Judging by the discussion on this thread from the libzip-discuss list it looks like you are trying to link against a static version of libzip but with the preprocessor symbol ZLIB_DLL defined. You should only have ZLIB_DLL defined if linking against the dll version.

Code Blocks, MinGW, Boost, and static linking issues

I am using Code Blocks with MinGW and am trying to get a simple program to compile with static linking. I have built the Boost libraries using these directions. Everything worked out fine and I was able to successfully compile this simple program (it compiles, I know it doesn't work because it exits before the message is sent to the console, but I just want it to compile).
If I have a DLL in my linker libraries, it compiles fine, but when I switch it with the static .a libraries of the same contents, I get undefined references such as "undefined reference to `_imp___ZN5boost6threadD1Ev'|".
I have no idea what the problem is and can't find the solution. I think it might have to do with linker settings but I can't find information on how to change them. I would be extremely grateful for any help that could be provided.
#include <iostream>
#include <boost/thread.hpp>
void myfunction()
{
std::cout << "this is a thread" << std::endl;
return;
}
int main()
{
boost::thread mythread(&myfunction);
return 0;
}
It's from trying to link statically when the headers are configured for a dynamic link. I explain this for libssh in this question. Poking around in boost/thread/detail/config.hpp makes me think you should #define BOOST_THREAD_USE_LIB, or use the -D flag to do the same.