0

I am able to run my C++ program on my local ubuntu vm. But when I try to compile it and run in a docker ubuntu:12.04/14.04/16.04/16.10 container or AWS EC2 ubuntu 16.04 vm, it gives Segmentation fault as soon as I run the program. When I try to debug using gdb : break main; run says during startup program exited normally

Following this question, I tried to compile it and run on different ubuntu versions containers:12.04,14.04,16.04,16.10. It gave same error in all of them.

CMakeLists.txt of my program for reference

cmake_minimum_required (VERSION 2.8)
include(GNUInstallDirs)
project (ferryfair)
IF (DEFINED _DEBUG)
ADD_DEFINITIONS(-D_DEBUG=${_DEBUG})
ENDIF()
set (VERSION_MAJOR 1)
set (VERSION_MINOR 0)
set (Umbrella "ferryfair")
IF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
# Mac OS X specific code
SET(macOS ON)
ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

set (GCC_COVERAGE_COMPILE_FLAGS "-std=c++11")
set (GCC_COVERAGE_LINK_FLAGS "-fPIC -Wl,-unresolved-symbols=ignore-in-shared-libs")
IF (DEFINED _DEBUG)
set (GCC_COVERAGE_COMPILE_FLAGS "${GCC_COVERAGE_COMPILE_FLAGS} -g")
set (GCC_COVERAGE_LINK_FLAGS "${GCC_COVERAGE_LINK_FLAGS} -g")
ENDIF()
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
file(GLOB HEADERS *.h)
file(GLOB SOURCES *.cpp)
include_directories(${PROJECT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_INSTALL_FULL_INCLUDEDIR}/${Umbrella})
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
target_link_libraries(${PROJECT_NAME} websockets FFJSON logger ferrybase pthread ssl crypto)
set_target_properties(${PROJECT_NAME}
    PROPERTIES
    VERSION ${VERSION_MAJOR}.${VERSION_MINOR}
    SOVERSION ${VERSION_MAJOR}
    )
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
install(FILES "${PROJECT_SOURCE_DIR}/config.json"
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}
    RENAME ${PROJECT_NAME}.json)
install(FILES "${PROJECT_SOURCE_DIR}/init.conf"
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/init
    RENAME ${PROJECT_NAME}.init)
install(FILES "${PROJECT_SOURCE_DIR}/init.override"
    DESTINATION /etc/init
    RENAME ${PROJECT_NAME}.override)

--Update-- segmentation fault is due to the error in my code and I fixed it. But I didn't understand why gdb doesn't break at main.

Community
  • 1
  • 1
Necktwi
  • 2,483
  • 7
  • 39
  • 62
  • try gdb? does ldd show everything is fine? – NG. Dec 02 '16 at 20:25
  • Perhaps you have an uninitialized pointer? Debuggers normally initialize data to zero, so e.g. a pointer becomes a null pointer. If you check for null pointers you will catch the that, but uninitialized data is otherwise *indeterminate* and might be seemingly random and most likely not null. – Some programmer dude Dec 02 '16 at 20:26
  • And you can have startup problems if you have global variables that performs undefined behavior on initialization. like e.g. using uninitialized pointers. – Some programmer dude Dec 02 '16 at 20:26
  • 2
    In C++, if you have global objects, their constructors will be invoked before `main`, and if there is an issue with the construction of such objects, you can cause things to happen such as seg faults. You need to debug your program. – PaulMcKenzie Dec 02 '16 at 20:28
  • Paul makes a good point. Might be useful to read about the [static initialization order fiasco](https://isocpp.org/wiki/faq/ctors#static-init-order) – AndyG Dec 02 '16 at 20:44

0 Answers0