Compiling program with CMake and Boost - compiles and links, but bad output and segfaults when run

StackOverflow https://stackoverflow.com/questions/14605009

سؤال

I'm having an issue when compiling a program with CMake 2.8.9 and Boost 1.49, on Ubuntu 12.10 (libboost installed via repos). Currently, the program is just some shared libs and an executable (which doesn't use the libs yet). The executable uses boost::program_options to parse input.

It is laid out like this (there will be more directories, such as a shared library and includes, but they don't feature in the CMake yet):

src
    |- tools
    |   |- CMakeLists.txt
    |   |- main.cpp   <-- main executable
    |- CMakeLists.txt

When I compile manually, it works, but when I CMake it, it corrupts some text and then segfaults at the end. The program code so far is simply exactly the "first" program from the program_options docs (you can see it here).


The manual compilation command I used is:

g++ -Wall -o myapp src/tools/main.cpp -lboost_program_options

which gives the correct output with myapp --help:

Allowed options:
  --help                produce help message
  --compression arg     set compression level

The CMake files look like this:

# src/CmakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(testApp CXX)

FIND_PACKAGE( Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})

ADD_DEFINITIONS(-g -O2
    -fsigned-char -freg-struct-return
    -Wall -W -Wshadow -Wpointer-arith -Wcast-qual -Winline -Werror)

ADD_SUBDIRECTORY(tools)
# src/tools/CmakeLists.txt
ADD_EXECUTABLE(main main.cpp)

TARGET_LINK_LIBRARIES(main
    ${Boost_LIBRARIES})

#rename to the final outut name
SET_TARGET_PROPERTIES(main
    PROPERTIES OUTPUT_NAME myapp)

When I then run myapp --help, I get the following output:

,@:
  --help                produce help message
  --compression arg     set compression level

Segmentation fault (core dumped)

The "@" symbol often changes to other, such as "`", "�" and Unicode-codepoints-in-boxes, which implies it is printing junk memory to me.

What am I doing wrong in this CMake setup?

هل كانت مفيدة؟

المحلول 2

This was caused by the use of the -freg-struct-return compiler flag. So nothing to do with CMake in the end, just careless code reuse. Thanks Bort for pointing me towards the compiler flags!

نصائح أخرى

My guess. The usage of add_definitions is wrong. It is intended for pre-processor macros not compiler options. I am not sure how this will interfere with the final output, but it is a starting point. Rewrite your cmakelists.txt file for your compiler options as given here.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top