1

I'm currently migrating a project from C to C++ and also would like to use cmake for project setup, but I don't know if the issue is actually related to cmake. Unfortunately I have not so much insight into lua details, it's right now just an obstacle for migrating.

I can reproduce the issue with the following sample:

#include <lauxlib.h>
#include <lualib.h>
#include <lua.h>

#include <iup.h>
#include <iuplua.h>
#include <iupcontrols.h>
#include <iupluacontrols.h>

#include <Windows.h>

lua_State *my_lua_init()
{
    lua_State *L;
    L = luaL_newstate();
    luaL_openlibs(L);
    lua_pop(L, 1);
    luaL_register(L, "my_project", NULL);
    lua_pop(L, 1);

    iuplua_open(L);
    iupcontrolslua_open(L);

    return L;
}

void my_lua_close(lua_State *L)
{
    if (L) 
        lua_close(L);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int cmdShow)
{
    lua_State *L = my_lua_init();

    my_lua_close(L);
    return 0;
}

and I get the following Linker Errors:

  1. error LNK2005: WinMain already defined in main.obj
  2. error LNK2019: unresolved external symbol main referenced in function WinMain

I use prebuilt static libraries for lua, iup, im_toolkit and canvasdraw for vc16 (VS2019) and 64bit. The application is also build with VS2019 and 64bit.

When I inspect the iup.lib, I indeed see the WinMain as a symbol there.

Now two question came to my mind:

  1. Why does the iup.lib defines a WinMain? Seems strange to me, as it is a library, and not a application.
  2. Is there a workaround to get the application properly linking?

Unfortunately I have to use the WinMain and can not change it to main. I already played around with target_link_libraries order in cmake, but got no working solution yet.

Thanks for help!

EDIT: normal main(...) is working, but that's not an option unfortunately.

int main(int argc, char** argv)
{
    lua_State *L = my_lua_init();

    my_lua_close(L);
    return 0;
}

EDIT2:

With some trial and error I figured out, that the problem seems to be somehow related that lua-related source file is compiled as C++ in the main project, and not C. Nevertheless for the small sample from above the problem occurs in both cases, compiling as C or C++. So it seems to be only halfway of the solution.

Feuerteufel
  • 571
  • 5
  • 16
  • 1
    iup is a GUI toolkit library. It's not unusual for that kind of library to define WinMain if the library provides its own kind of main function. you should check uip on how to use it – Brecht Sanders May 27 '21 at 15:43
  • "I don't know if the issue is actually related to cmake" - Without viewing your CMake code (`CMakeLists.txt`) it impossible for use check, whether your issue is related to CMake or not. Please, add you `CMakeLists.txt` into the question post too. – Tsyvarev May 27 '21 at 15:59
  • @BrechtSanders: According to their [example](https://www.tecgraf.puc-rio.br/iup/) in "Tutorial/Hello World", the example defines its **own** `main` and **successfully** links with `iup`. – Tsyvarev May 27 '21 at 16:02
  • Have you tried also writing a main() instead of WinMain() function and linking with `-mconsole` ? – Brecht Sanders May 27 '21 at 16:14
  • As already written, unfortunately I can't switch to main, because the real application I'm migrating depends on the arguments of WinMain and for now I can not refactor that one. – Feuerteufel May 27 '21 at 18:05

1 Answers1

0

IUP defines a WinMain to allow the application to be fully portable by using a "main" declaration instead. So simply use main instead of WinMain, as commented above, but it is not necessary to use "-mconsole".

Antonio Scuri
  • 1,046
  • 6
  • 10
  • unfortunately "simply" use main is not possible right now, I have to stick to WinMain as entry point for the application I'm migrating. – Feuerteufel May 27 '21 at 18:06
  • I guess that means that you'll have to recompile iup.lib yourself and remove WinMain or change its logic to whatever works for you. – Paul Kulchenko May 28 '21 at 15:11
  • When not using -mconsole, the internal WinMain will be called, then your main function will be called from there, so I believe that you can move the code you have in your WinMain to the main function, with some adapting of course. This is probably easier than rebuilding the library your self. – Antonio Scuri May 29 '21 at 16:54
  • Notice that when using IUP in a DLL, the WinMain function is left out of it in a stub library. – Antonio Scuri May 29 '21 at 16:55