定义shared_ptr导致segfault(CMake) [英] defining shared_ptr causes segfault (CMake)

查看:352
本文介绍了定义shared_ptr导致segfault(CMake)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置一个新项目时(使用CMake,编译器是gcc版本5.2.1,ubuntu(15.10)),我想使用shared_ptr。



simple main.cpp work fine:

  #include< iostream> 
#include< memory>
using namespace std;

int main()
{
cout<<Hi there!<<
return 0;
}

但是,只是定义一个shared_ptr会导致程序崩溃与segfault, Hi there!。

  #include< iostream> 
#include< memory>
using namespace std;

int main()
{
cout<<Hi there!<< endl;
shared_ptr< double>测试; // < - new line
return 0;
}

我添加了

  set(CMAKE_CXX_FLAGS-std = c ++ 11)

到CMakeLists.txt。有什么我在这里失踪。我找不到任何解释segfault的答案,只是因为一个shared_ptr的定义。



GDB输出根本没有帮助:

 编程接收信号SIGSEGV,分段故障。 
0x0000000000000000 in ?? ()

EDIT:
使用



  g ++ -std = c ++ 11 -o testx main.cpp 

为这两种情况生成一个可运行的可执行文件,所以它必须是一个CMake问题。所以这里是项目的CMake文件:

 项目(yorld3)
cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS-std = c ++ 11)

find_package(OpenGL REQUIRED)
include_directories($ {OpenGL_INCLUDE_DIRS})
set {LIBS} $ {OpenGL_LIBRARIES})

find_package(GLUT REQUIRED)
include_directories($ {GLUT_INCLUDE_DIRS})
set(LIBS $ {LIBS} $ {GLUT_LIBRARIES})

find_package(Bullet REQUIRED)
include_directories($ {Bullet_INCLUDE_DIRS})
set(LIBS $ {LIBS} $ {Bullet_LIBRARIES})

link_directories SRC_BINARY_DIR} / src)

add_subdirectory(src)
INCLUDE_DIRECTORIES(src)
INCLUDE_DIRECTORIES(src / core)
add_executable(test main.cpp)
target_link_libraries(test mycorelib GLU GL glut)

EDIT2:
经过很多测试后我手动编译程序再次没有链接我的lib:

  g ++ -std = c ++ 11 -g -Wall -I src / core / app -o testx main.cpp src / core / app / yorld_window.cpp -lGL -lGLU -lglut 

这样我可以不使用CMake重现segfault。

解决方案

挖掘我发现问题是与CMake无关。对另一个问题的评论是解决方案! p>

结果是您必须链接到 pthread


-lpthread


或只是


target_link_libraries (测试... pthread


我仍然发现这是奇怪的,这是没有被链接器检测到!


While setting up a new project (using CMake, compiler is gcc version 5.2.1, ubuntu (15.10)), I wanted to use a shared_ptr.

This simple main.cpp works fine:

#include <iostream>
#include <memory>    
using namespace std;

int main()
{    
    cout<<"Hi there!"<<endl;
    return 0;
}

But just defining a shared_ptr will cause the program to crash with segfault before even writing "Hi there!".

#include <iostream>
#include <memory>    
using namespace std;

int main()
{    
    cout<<"Hi there!"<<endl;
    shared_ptr<double> test;  // <- new line
    return 0;
}

I added

set(CMAKE_CXX_FLAGS "-std=c++11")

to the CMakeLists.txt. Is there something I'm missing here. I could not find any answers that explain the segfault just because of the definition of a shared_ptr.

The GDB output is not helpful at all:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()

EDIT: Compiling by hand using

g++ -std=c++11 -o testx main.cpp

produces a runable executable for both cases so it has to be a CMake issue i guess. So here is the CMake file for the project:

project(yorld3)
cmake_minimum_required(VERSION 2.8)

set(CMAKE_CXX_FLAGS "-std=c++11")

find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
set(LIBS ${LIBS} ${OpenGL_LIBRARIES})

find_package(GLUT REQUIRED)
include_directories(${GLUT_INCLUDE_DIRS})
set(LIBS ${LIBS} ${GLUT_LIBRARIES})

find_package(Bullet REQUIRED)
include_directories(${Bullet_INCLUDE_DIRS})
set(LIBS ${LIBS} ${Bullet_LIBRARIES})

link_directories(${SRC_BINARY_DIR}/src)

add_subdirectory(src)
INCLUDE_DIRECTORIES(src)
INCLUDE_DIRECTORIES(src/core)
add_executable(test main.cpp )
target_link_libraries(test mycorelib GLU GL glut)

EDIT2: After a lot of testing I manually compiled the program again not linking my lib:

g++ -std=c++11 -g -Wall -I src/core/app -o testx main.cpp src/core/app/yorld_window.cpp -lGL -lGLU -lglut

That way I can reproduce the segfault without using CMake.

解决方案

After a lot more code digging I found out the problem was not related to CMake. A comment to an other question was the solution!

Turns out you have to link to pthread

-lpthread

or just

target_link_libraries(test ... pthread)

That way shared pointers work with glut. I still find it strange that this is in no way detected by the linker!

这篇关于定义shared_ptr导致segfault(CMake)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆