Skip to content

C++:Troubleshooting

C/C++와 관련된 모든 문제점에 대하여 정리한다.

Format security warning

아래와 같은 경고가 출력될 경우.

warning: format string is not a string literal (potentially insecure) [-Wformat-security]
    printf(print_string.c_str());

아래와 같이 수정하면 된다.

printf("%s", print_string.c_str());

HEAP CORRUPTION DETECTED

HEAP CORRUPTION DETECTED 에러의 원인은 malloc이나 new로 할당한 메모리의 영역보다 더 큰 영역에 접근하고자 했기 때문이다.

malloc()으로 할당한 메모리의 정렬상태 또는 크기를 정확히 확인해야 한다.

Stackoverflow in function

함수 시작 지점에서 Stackoverflow에러가 발생할 경우 MSVC의 경우 /STACK옵션을 사용해 스택 크기를 늘려주면 된다.

direct access in ~ to global weak symbol

ld컴파일 경고가 아래와 같이 출력될 수 있다.

ld: warning: direct access in _main to global weak symbol std::__1::char_traits::eq(char, char) means the weak symbol cannot be overridden at runtime. This was likely caused by different translation units being compiled with different visibility settings.

See Controlling Symbol Visibility @ developer.apple.com for details.

It looks like your libs (eg. the C++ standard library) and your code have been compiled with different visibility settings, at least, that is what the linker error message is saying.

To fix the warning, you should use the same visibility settings when compiling your code, eg -fvisibility=hidden

즉, 링크되는 모든 라이브러리의 가시설 설정(Visibility setting)이 모두 동일해야 한다.

Hide terminal window

어플리케이션 실행시 터미널 윈도우가 출력되는 현상을 해결 하기 위한 방법.

MinGW (Windows)

링커옵션에 링커옵션에 -Wl,--subsystem,windows또는 -mwindows를 추가하면 된다.

macOS

Based on your description, you're building what is a unix-style executable. On OS X, those will always launch inside of a terminal window. The choices that you have on OS X are:

  • Run as a daemon as described in the above-linked post
  • Run in Terminal as a unix executable
  • Create a minimal OS X application wrapper and run as an OS X Application

In most cases, you can create a wrapper for a unix-style executable by creating the appropriate Bundle using the instructions from Apple's Bundle Programming Guide (skip over the iOS stuff and look at the Mac bundle information).

The basic directory structure is:

MyApp.app/
  Contents/
    Info.plist
  MacOS/
    executable
  Resources/
    MyApp.icns

Your unmodified executable can go in the MacOS directory, and you'll need to set up the following keys in the Info.plist using a plist editing tool or editor:

  • CFBundleIdentifier - the id of your app in reverse-dns notation (com.mycompany.myapp)
  • CFBundleDisplayName- the name of your app in human-readable form (MyApp)
  • CFBundleName - the short name of the app (usually the same as your app and executable name)
  • CFBundleVersion - your version # in X.Y[.Z] form
  • CFBundlePackageType - the package type, which should be APPL for applications
  • CFBundleExecutable - the name of your executable
  • CFBundleSignature - old-school Apple signature (4 character code that should theoretically be registered with apple)

A minimal plist would look like this: CFBundleDisplayName MyApp CFBundleExecutable a.out CFBundleIdentifier com.mycompany.myapp CFBundleName MyApp CFBundlePackageType APPL CFBundleSignature FOOZ CFBundleVersion 1.0

(The above example uses a.out as the executable, which would be located in MyApp.app/Contents/MacOS/a.out)

The icon resources can be left out if you don't care about the icon, and the default application icon will be used.

std::ios_base::badbit

Memory shortage.
There is no memory available to create the buffer, or the buffer has size 0 for other reasons (such as being provided from outside the stream), or the stream cannot allocate memory for its own internal data, as with std::ios_base::iword() and std::ios_base::pword().
The underlying stream buffer throws an exception.
The stream buffer might lose its integrity, as in memory shortage, or code conversion failure, or an unrecoverable read error from the external device. The stream buffer can indicate this loss of integrity by throwing an exception, which is caught by the stream and results in setting the badbit in the stream's state.

stdint.h 또는 inttypes.h 파일을 찾을 수 없을 경우

msinttypes를 다운받는다.

error: 'UINT64_C' was not declared in this scope

오류가 나올경우 컴파일 옵션에 CXXFLAGS=-D__STDC_CONSTANT_MACROS을 추가해 주면 된다. 아니면 헤더파일에 아래 항목을 추가해 줄수도 있다.

#ifdef __cplusplus
#define __STDC_CONSTANT_MACROS
#  ifdef _STDINT_H
#undef _STDINT_H
#  endif
#include <stdint.h>
#endif

See also