Я скопировал код hello world в мой основной c ++, надеясь, что смогу постепенно его модифицировать, чтобы медленно создавать рендеринг. Я могу скомпилировать его как в режиме выпуска, так и в режиме отладки. Но когда я запускаю его, он работает в режиме отладки и завершается с ошибкой в выпуске. Я получаю ошибку сегментации.
Моя установка — проект cmake в netbeans. Мой 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" .
Я установил vtk, используя dnf на Fedora 25: 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";
}
}
Помните, что он отлично работает в режиме отладки.
Конфликтующей строкой является начало графического интерфейса, а именно renderWindowInteractor->Start();
Я попытался скопировать команды debug cmake в релизную, но это не сработало. Здесь действует своего рода скрытое поведение, которое я не могу обнаружить!
РЕДАКТИРОВАТЬ:
Вот результат, который вы предложили посмотреть. Кажется, вопрос ВТК? `
==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.
`
Следующие изменения в файле CMakeList.txt сделали его запущенным. Это решение, но оно не объясняет почему. Если кто-нибудь может уточнить, почему это решение, пожалуйста, сделайте.
Мне пришлось изменить способ связать библиотеки. поэтому я убрал строку
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})
и изменил его в
if(CMAKE_BUILD_TYPE MATCHES "Debug")
target_link_libraries(uvlm ${GTE_DEBUG_CXX_LIBRARY} ${USEFULFCTS_DEBUG_CXX_LIBRARY} ${VTK_LIBRARIES})
else()
target_link_libraries(uvlm ${GTE_RELEASE_CXX_LIBRARY} ${USEFULFCTS_RELEASE_CXX_LIBRARY} ${VTK_LIBRARIES})
endif()
Других решений пока нет …