对std :: bad_cast :: bad_cast()的未定义引用

时间:2014-03-08 01:20:03

标签: c++ cmake clang linker-errors undefined-reference

我在linux上使用带有clang ++的cmake。我遇到了一些连接器问题:

Linking CXX executable "/run/media/toi/Storage E/Dropbox/Programmering/Flictus/bin/main"
CMakeFiles/main.dir/GraphicsEngine/Shader.cpp.o:/run/media/toi/Storage E/Dropbox/Programmering/Flictus/src/GraphicsEngine/Shader.cpp:function std::__1::basic_filebuf<char, std::__1::char_traits<char> >::seekoff(long long, std::__1::ios_base::seekdir, unsigned int): error: undefined reference to 'std::bad_cast::bad_cast()'
CMakeFiles/main.dir/GraphicsEngine/Shader.cpp.o:/run/media/toi/Storage E/Dropbox/Programmering/Flictus/src/GraphicsEngine/Shader.cpp:function std::__1::basic_filebuf<char, std::__1::char_traits<char> >::sync(): error: undefined reference to 'std::bad_cast::bad_cast()'
CMakeFiles/main.dir/GraphicsEngine/Shader.cpp.o:/run/media/toi/Storage E/Dropbox/Programmering/Flictus/src/GraphicsEngine/Shader.cpp:function std::__1::basic_filebuf<char, std::__1::char_traits<char> >::underflow(): error: undefined reference to 'std::bad_cast::bad_cast()'
CMakeFiles/main.dir/GraphicsEngine/Shader.cpp.o:/run/media/toi/Storage E/Dropbox/Programmering/Flictus/src/GraphicsEngine/Shader.cpp:function std::__1::basic_filebuf<char, std::__1::char_traits<char> >::overflow(int): error: undefined reference to 'std::bad_cast::bad_cast()'

我认为它确实是一个链接器问题,因为它在链接开始后出现并且包含了相关的头文件。

我没有在std::cout等上获得未定义的引用。

生成链接器错误的代码:

std::ifstream file(shaderFile, std::ios::in | std::ios::binary);
if (file) {
    file.seekg(0, std::ios::end);
    shaderString.resize(file.tellg());
    file.seekg(0, std::ios::beg);
    file.read(&shaderString[0], shaderString.size());
    file.close();
}

项目运行良好,直到添加上面的代码。它在Visual Studio 2013中运行没有问题。

cmake文件的相关部分:

add_compile_options(-stdlib=libc++)
add_compile_options(-std=c++1y)
set(CMAKE_LINKER /usr/bin/clang++) # didn't help when added
set(CMAKE_EXE_LINKER_FLAGS "-lc++ -Wall -fexceptions")
set(CMAKE_CXX_COMPILER /usr/bin/clang++)
set(CMAKE_C_COMPILER /usr/bin/clang)

那么我应该链接哪些内容,还是clang ++中的限制?

编辑:链接器调用:

"/usr/bin/ld.gold" -export-dynamic --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o 
/run/media/toi/Storage E/Dropbox/Programmering/Flictus/bin/main /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib64/crt1.o /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib64/crti.o 
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/crtbegin.o -L/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2 -L/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../.. -L/lib
 -L/usr/lib CMakeFiles/main.dir/main.cpp.o CMakeFiles/main.dir/GraphicsEngine/GraphicsEngine.cpp.o CMakeFiles/main.dir/GraphicsEngine/Shader.cpp.o 
CMakeFiles/main.dir/GraphicsEngine/TextureLoader.cpp.o CMakeFiles/main.dir/Input/Gamepad.cpp.o CMakeFiles/main.dir/Input/Input.cpp.o CMakeFiles/main.dir/GUI/Font.cpp.o CMakeFiles/main.dir/GUI/GUIEngine.cpp.o 
CMakeFiles/main.dir/GUI/GUIRect.cpp.o CMakeFiles/main.dir/GUI/GUISprite.cpp.o CMakeFiles/main.dir/GUI/Objects/Components/GUIText.cpp.o CMakeFiles/main.dir/GUI/Objects/GUIButton.cpp.o 
CMakeFiles/main.dir/GUI/Objects/GUIImage.cpp.o CMakeFiles/main.dir/GUI/Objects/GUILabel.cpp.o CMakeFiles/main.dir/GUI/Objects/GUIListBox.cpp.o 
CMakeFiles/main.dir/GUI/Objects/GUIObject.cpp.o -lSDL2 -lpthread -lGLEW -lGL -lSOIL -lc++ -lfreetype -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/crtend.o /usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.8.2/../../../../lib64/crtn.o

2 个答案:

答案 0 :(得分:1)

通过与libc++abi关联解决了这个问题。

答案 1 :(得分:0)

如果您还没有这样做,请尝试使用前缀g ++而不是gcc EX编译代码:

g++ (file name) :  gcc (file name)

第一个是正确的。

原因是因为使用g ++前缀允许运行不同的代码库。