Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strdup undeclared under MinGW with -std=c++11 in imgui_demo #520

Closed
Teybeo opened this issue Feb 5, 2016 · 8 comments
Closed

Strdup undeclared under MinGW with -std=c++11 in imgui_demo #520

Teybeo opened this issue Feb 5, 2016 · 8 comments

Comments

@Teybeo
Copy link

Teybeo commented Feb 5, 2016

Hi,
I just decided to give a try to this lib as it looks super cool and I only encoutered one compilation error so I though I should report it before forgetting about it 😃

In Clion, after copying and defining all the root files in the CMakeLists.txt, i got these errors:

D:\Dev\Sandbox\Imgui demo\imgui_demo.cpp: In member function 'void ExampleAppConsole::AddLog(const char*, ...)':
D:\Dev\Sandbox\Imgui demo\imgui_demo.cpp:1874:37: error: 'strdup' was not declared in this scope
         Items.push_back(strdup(buf));
                                     ^
D:\Dev\Sandbox\Imgui demo\imgui_demo.cpp: In member function 'void ExampleAppConsole::ExecCommand(const char*)':
D:\Dev\Sandbox\Imgui demo\imgui_demo.cpp:1967:48: error: 'strdup' was not declared in this scope
         History.push_back(strdup(command_line));
                                                ^

My env is MinGW on Win7 with GCC 4.8.1
This function seems to be from POSIX standard but not from C/C++.

Looking at the code I found the ImStrdup() function declared in imgui_internal.h and defined in imgui.cpp.
Changing the 2 strdup to ImStrdup and adding #include "imgui_internal.h" in imgui_demo.cpp made the trick but I'm unsure if including the whole internal header is the best fix for this.

PS: I also had to add -limm32 to the compiler flag to make it link.

@Teybeo Teybeo changed the title Trivial strdup undeclared error in imgui_demo MinGW Trivial strdup undeclared error in imgui_demo (MinGW) Feb 5, 2016
@ocornut
Copy link
Owner

ocornut commented Feb 5, 2016

I don't know which version you are looking at, but those ImStrdup were replaced with strdup a long time ago.

The opengl_example* Makefile already have -limm32 for mingw. Where did you have to add it?
You can define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS in imconfig.h to disable IME support and the need to link with it.

@Teybeo
Copy link
Author

Teybeo commented Feb 5, 2016

Sorry, i messed up my original message, now edited, the compiler error was about strdup not being declared (I copied the output after changing strdup to ImStrdup).


About imm32 I just copied the 3 sdl_opengl_example files and setup this CMakeLists.txt for building on Clion:

cmake_minimum_required(VERSION 3.4)
project(Imgui_demo)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

########### SDL paths ############

set(SDL_INCLUDE_DIR "D:/Dev/Lib/SDL2-2.0.3/i686-w64-mingw32/include/SDL2")
set(SDL_LIB_DIR "D:/Dev/Lib/SDL2-2.0.3/i686-w64-mingw32/lib")

find_library(SDL_LIBRARY
        NAMES SDL2
        HINTS ${SDL_LIB_DIR}
        NO_DEFAULT_PATH)

#################################

list(APPEND LIBRARIES ${SDL_LIBRARY})
list(APPEND LIBRARIES opengl32)
list(APPEND LIBRARIES imm32)

set(SOURCE_FILES main.cpp)
list(APPEND SOURCE_FILES imgui.cpp imgui.h)
list(APPEND SOURCE_FILES imgui_impl_sdl.cpp imgui_impl_sdl.h)
list(APPEND SOURCE_FILES imgui_demo.cpp)
list(APPEND SOURCE_FILES imgui_internal.h)
list(APPEND SOURCE_FILES imgui_draw.cpp)
list(APPEND SOURCE_FILES stb_rect_pack.h)
list(APPEND SOURCE_FILES stb_textedit.h)
list(APPEND SOURCE_FILES stb_truetype.h)

include_directories(${SDL_INCLUDE_DIR})
add_executable(Imgui_demo ${SOURCE_FILES})

target_link_libraries("Imgui_demo" ${LIBRARIES})

@ocornut
Copy link
Owner

ocornut commented Feb 5, 2016

Where is strdup declared with your compiler/system?

@Teybeo
Copy link
Author

Teybeo commented Feb 5, 2016

Ok I should have searched a bit more, when using the -std=c++11 flag, in string.h, the strdup and several other functions are ifdef'ed out under STRICT_ANSI

@Teybeo Teybeo changed the title Trivial strdup undeclared error in imgui_demo (MinGW) Strdup undeclared under MinGW with -std=c++11 in imgui_demo Feb 5, 2016
ocornut added a commit that referenced this issue Feb 8, 2016
@ocornut
Copy link
Owner

ocornut commented Feb 8, 2016

What that's another annoyance caused by MinGW and modern compilers I suppose :/
Now switched to use a local function.

@ocornut
Copy link
Owner

ocornut commented Feb 8, 2016

About imm32: the docs are for Visual Studio but the Makefile for MinGW in OpenGL examples have the imm32 library. We could have an extra command-line example for MinGW if you want to submit a PR that cover various examples.

Ideally we have a better build system across all examples. cmake is just too ugly with the amount of temporary files it creates.

@ocornut ocornut closed this as completed Feb 8, 2016
ocornut added a commit that referenced this issue Oct 24, 2017
…32_DEFAULT_IME_FUNCS to IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS/IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS for consistency. (ref #238, #520, #738)
@ocornut
Copy link
Owner

ocornut commented Oct 24, 2017

FYI, leaving a note here for potential future readers stumbling here:

I have renamed those two defines that are usable (but very rarely used) in imconfig.h
IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS became IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS.
IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCS became IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS.

Very few people will be affected and they will likely get a link error and therefore will notice.

The discussion in this thread pertain to using GCC/MinGW and both have those functions disabled already, so GCC/MinGW shouldn't be affected.

@mnba
Copy link

mnba commented Jan 30, 2019

Ok I should have searched a bit more, when using the -std=c++11 flag, in string.h, the strdup and several other functions are ifdef'ed out under STRICT_ANSI

Not exactly STRICT_ANSI but STRICT_ANSI
The simplest solution I found for similar problem is ##undef it before including <pybind11/pybind11.h>, like this:

#undef  __STRICT_ANSI__
#include <pybind11/pybind11.h>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants