我将 hello world 代码复制到我的主 c++ 中,希望能够一点一点地修改它,以便慢慢创建我的渲染。我能够在发布和调试模式下编译它。但是当我运行它时,它在调试中工作并且在发布时失败。我得到一个分段错误。
我的设置是 netbeans 中的一个 cmake 项目。我的 CMakeList.txt 看起来像这样:
cmake_minimum_required(VERSION 3.6)
project(uvlm)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES
... hidden for clarity ...)
include_directories(.
${EIGEN_LIBRARY_PATH}
${GITREPO_PATH}/hydros-mathlibrary/HydrosTools/UsefullCppFcts
${GITREPO_PATH}/hydros-mathlibrary/HydrosMathLibraryCode/Include/
)
if(CMAKE_BUILD_TYPE MATCHES "Debug")
set(EXECUTABLE_OUTPUT_PATH ${GITREPO_PATH}/hydros-uvlm/uvlm/Debug/bin)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} -std=c++14 -D__LINUX__ ")
elseif(CMAKE_BUILD_TYPE MATCHES "Profiling")
set(EXECUTABLE_OUTPUT_PATH ${GITREPO_PATH}/hydros-uvlm/uvlm/Profiling/bin)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -std=c++14 -D__LINUX__ ")
else()
set(EXECUTABLE_OUTPUT_PATH ${GITREPO_PATH}/hydros-uvlm/uvlm/Release/bin)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} -std=c++14 -D__LINUX__ ")
endif()
# DEBUG LIBRARIES
find_library(GTE_DEBUG_CXX_LIBRARY NAMES libgtengine.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosMathLibraryCode/lib/Debug)
find_library(USEFULFCTS_DEBUG_CXX_LIBRARY NAMES libusefulfcts.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosTools/UsefullCppFcts/lib/Debug)
# RELEASE LIBRARIES
find_library(GTE_RELEASE_CXX_LIBRARY NAMES libgtengine.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosMathLibraryCode/lib/Release)
find_library(USEFULFCTS_RELEASE_CXX_LIBRARY NAMES libusefulfcts.a HINTS ${GITREPO_PATH}/hydros-mathlibrary/HydrosTools/UsefullCppFcts/lib/Release)
FIND_PACKAGE( OpenMP)
if(OPENMP_FOUND)
message("OPENMP FOUND")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
include_directories(SYSTEM ${OpenMP_INCLUDE_PATH})
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(uvlm ${SOURCE_FILES})
target_link_libraries(uvlm debug ${GTE_DEBUG_CXX_LIBRARY} ${USEFULFCTS_DEBUG_CXX_LIBRARY} ${VTK_LIBRARIES} optimized ${GTE_RELEASE_CXX_LIBRARY} ${USEFULFCTS_RELEASE_CXX_LIBRARY} ${OpenMP_CXX_LIBRARIES} ${VTK_LIBRARIES})
我的 cmake 命令行具有如下参数:
${CMAKE} -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=${IDE_CC} -DCMAKE_CXX_COMPILER=${IDE_CXX} -DCMAKE_C_FLAGS_RELEASE="-O3 -DEIGEN_NO_DEBUG -march=native -mfpmath=sse -funroll-loops" -DCMAKE_CXX_FLAGS_RELEASE=" -O3 -mfpmath=sse -funroll-loops -DEIGEN_NO_DEBUG " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DEIGEN_LIBRARY_PATH="HIDDENPATH" -DGITREPO_PATH="HIDDENPATH" .
我在 Fedora 25 上使用 dnf 安装了 vtk:vtk-devel-6.3.0-11.fc25.x86_64
我运行的代码如下:
void vtkRenderingTest(std::string fileName, bool mbdynInfiniteLoop)
{
// This creates a polygonal cylinder model with eight circumferential facets
// (i.e, in practice an octagonal prism).
vtkSmartPointer<vtkCylinderSource> cylinder =
vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(8);
// The mapper is responsible for pushing the geometry into the graphics library.
// It may also do color mapping, if scalars or other attributes are defined.
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
// The actor is a grouping mechanism: besides the geometry (mapper), it
// also has a property, transformation matrix, and/or texture map.
// Here we set its color and rotate it around the X and Y axes.
vtkSmartPointer<vtkActor> cylinderActor =
vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
cylinderActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
cylinderActor->RotateX(30.0);
cylinderActor->RotateY(-45.0);
// The renderer generates the image
// which is then displayed on the render window.
// It can be thought of as a scene to which the actor is added
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cylinderActor);
renderer->SetBackground(0.1, 0.2, 0.4);
// Zoom in a little by accessing the camera and invoking its "Zoom" method.
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.5);
// The render window is the actual GUI window
// that appears on the computer screen
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(200, 200);
renderWindow->AddRenderer(renderer);
// The render window interactor captures mouse events
// and will perform appropriate camera or actor manipulation
// depending on the nature of the events.
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);
// This starts the event loop and as a side effect causes an initial render.
renderWindowInteractor->Start();
}
int main(int argc, char* argv[])
{
try {
// put the file name in the run property of the configurations (Release and Debug)
bool mbdynInfiniteLoop = false;
// if (argc > 2) { if (argv[2] == "true") { mbdynInfiniteLoop = true; } }
// loadsEval(argv[1], mbdynInfiniteLoop);
vtkRenderingTest("AAAA", true);
return 0;
}
catch (std::string &e) {
std::cout << "############### Error #####################\n\n";
std::cout << e << std::endl;
std::cout << "\n###########################################\n\n";
}
catch (hyd::HydExceptions &e) {
std::cout << "############### Error #####################\n\n";
std::cout << e.errMessage << std::endl;
std::cout << "\n###########################################\n\n";
}
}
请记住,它在调试模式下完美运行。
冲突的行是 GUI 的开始,即renderWindowInteractor->Start();
我试图将调试 cmake 命令复制到发行版之一,但它没有用。一种我无法发现的隐藏行为在这里起作用!
编辑:这是您建议查看的输出。这似乎是一个VTK问题?`
==3082== Process terminating with default action of signal 11 (SIGSEGV)
==3082== Access not within mapped region at address 0x0
==3082== at 0x1FAD435F: rawmemchr (in /usr/lib64/libc-2.24.so)
==3082== by 0x1FABC831: _IO_str_init_static_internal (in /usr/lib64/libc-2.24.so)
==3082== by 0x1FAAE7E6: vsscanf (in /usr/lib64/libc-2.24.so)
==3082== by 0x1FAA8A26: sscanf (in /usr/lib64/libc-2.24.so)
==3082== by 0x143292B3: vtkOpenGLExtensionManager::ReadOpenGLExtensions() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x143283CD: vtkOpenGLExtensionManager::Update() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x14323BA3: vtkOpenGLExtensionManager::ExtensionSupported(char const*) (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x14371259: vtkOpenGLRenderWindow::OpenGLInitContext() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x1436FBCC: vtkOpenGLRenderWindow::OpenGLInit() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x1444238C: vtkXOpenGLRenderWindow::Start() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x144377BF: vtkXRenderWindowInteractor::Initialize() (in /usr/lib64/vtk/libvtkRenderingOpenGL.so.1)
==3082== by 0x1B2175A2: vtkRenderWindowInteractor::Start() (in /usr/lib64/vtk/libvtkRenderingCore.so.1)
==3082== If you believe this happened as a result of a stack
==3082== overflow in your program's main thread (unlikely but
==3082== possible), you can try to increase the size of the
==3082== main thread stack using the --main-stacksize= flag.
==3082== The main thread stack size used in this run was 8388608.
`