0

I download SDK from official site, and create new project with following commands:

cocos new foo -p com.foo.bar -l cpp -d .

So, new project foo created and I enter its directory. I know proj.win32 has solution files for visual studio prebuilt in it. But I want regenerate them from CMakeLists.txt at root project directory.

So I mkdir -p cmake-out/vs2012, enter directory , and cmake -G "Visual Studio 11 2012" ../../. Everything's fine, solution files are built by cmake, so I rebuilt whole solution in visual studio. Compilation all passed*, but final link process is fail because following errors:

4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '___WSAFDIsSet@8' referenced in function '"protected: void __thiscall cocos2d::Console::loop(void)" (?loop@Console@cocos2d@@IAEXXZ)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__accept@12' referenced in function '"protected: void __thiscall cocos2d::Console::addClient(void)" (?addClient@Console@cocos2d@@IAEXXZ)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__bind@12' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__closesocket@4' referenced in function '"protected: void __thiscall cocos2d::Console::commandExit(int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?commandExit@Console@cocos2d@@IAEXHABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__ioctlsocket@12' referenced in function '"protected: void __thiscall cocos2d::Console::loop(void)" (?loop@Console@cocos2d@@IAEXXZ)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__listen@8' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__ntohs@4' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__recv@16' referenced in function '"protected: void __thiscall cocos2d::Console::commandUpload(int)" (?commandUpload@Console@cocos2d@@IAEXH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__select@20' referenced in function '"protected: void __thiscall cocos2d::Console::loop(void)" (?loop@Console@cocos2d@@IAEXXZ)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__send@16' referenced in function '"public: void __thiscall <lambda_18a69eabc233d9cfc5fb2fb65c07cfe6>::operator()(void)const " (??R<lambda_18a69eabc233d9cfc5fb2fb65c07cfe6>@@QBEXXZ)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__setsockopt@20' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__socket@12' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__WSAStartup@8' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__WSACleanup@0' referenced in function '"protected: void __thiscall cocos2d::Console::loop(void)" (?loop@Console@cocos2d@@IAEXXZ)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__getaddrinfo@16' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '__imp__freeaddrinfo@4' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'
4>cocos2d.lib(CCConsole.obj) : error LNK2019: unresolved external symbol '_inet_ntop@16' referenced in function '"public: bool __thiscall cocos2d::Console::listenOnTCP(int)" (?listenOnTCP@Console@cocos2d@@QAE_NH@Z)'

Original solution have no errors, I know. But visual studio solution files should be able to generated by cmake, What problem with CMakeLists.txt ?


*: In fact, include directories settings aren't correct, so I patch them:

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/Classes
    ${COCOS2D_ROOT}
    ${COCOS2D_ROOT}/cocos
    ${COCOS2D_ROOT}/cocos/audio/include
    ${COCOS2D_ROOT}/cocos/2d
    ${COCOS2D_ROOT}/cocos/2d/renderer
    ${COCOS2D_ROOT}/cocos/2d/platform
    ${COCOS2D_ROOT}/cocos/2d/platform/desktop
    ${COCOS2D_ROOT}/cocos/2d/platform/win32
    ${COCOS2D_ROOT}/cocos/base

with them:

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/Classes
    ${COCOS2D_ROOT}
    ${COCOS2D_ROOT}/cocos
    ${COCOS2D_ROOT}/cocos/audio/include
    ${COCOS2D_ROOT}/cocos/2d
    ${COCOS2D_ROOT}/cocos/renderer
    ${COCOS2D_ROOT}/cocos/platform
    ${COCOS2D_ROOT}/cocos/platform/desktop
    ${COCOS2D_ROOT}/cocos/platform/win32
    ${COCOS2D_ROOT}/cocos/base

Yes, renderer,platform... not in cocos/2d path. That's the only change I must do to the sources.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
naive231
  • 1,360
  • 1
  • 11
  • 28
  • 1
    possible duplicate of [Failure to link against appropriate libraries/object files or compile implementation files](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574400#12574400) "In Windows programming, the tell-tale sign that you did not link a necessary library is that the name of the unresolved symbol begins with __imp_." – Raymond Chen Sep 21 '14 at 06:28
  • @RaymondChen: It is not a duplicate, it is cocos2d-specific problem. Basically, sdk is supposed to generate proper solution for you, but fails to do so. – SigTerm Dec 18 '14 at 09:11
  • The solution is the same: Add the missing libraries. – Raymond Chen Dec 18 '14 at 09:39

1 Answers1

0

In case somebody ever stumbles into this particular issue again.

It happens because cocos2d cmakelists.txt fails to link with ws2_32 library on windows platofrm. Apparently nobody ever uses cocos2d-x for windows development or something? Anyway.

To fix the problem, locate target_link_libraries section in your CMakeLists.txt, and change it.

Look for:

target_link_libraries(${APP_NAME}
  spine
  cocostudio
  cocosbuilder
  extensions
  audio
  cocos2d
  )

And replace it with:

if(WIN32 AND MSVC)
  target_link_libraries(${APP_NAME}
    spine
    cocostudio
    cocosbuilder
    extensions
    audio
    cocos2d
    box2d
    ws2_32
  )
else()
  target_link_libraries(${APP_NAME}
    spine
    cocostudio
    cocosbuilder
    extensions
    audio
    cocos2d
    box2d
  )
endif()

Hopefully that'll fix this particular problem and the whole thing will compile without running into another issue.

SigTerm
  • 26,089
  • 6
  • 66
  • 115