Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Trying to manage Hot loading code with SFML  (Read 3677 times)

0 Members and 1 Guest are viewing this topic.

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Trying to manage Hot loading code with SFML
« on: August 11, 2019, 09:49:12 pm »
Im trying to implement hot loading with SFML.

I manage to do it with a normal C Console application and the process goes like so:

1. create some code and compile it into a .DLL

2. load the code in my main-loop


However when i try to make a DLL with some SFML ckasses/functions I get a lot of unresolved externals, and i can't really understand how this would be solved.

any ideas??

first problem is making a DLL that has some classes/functions from the SFML-library.

gameCode.h:
#include "SFML\Graphics.hpp"

extern "C"
{
        __declspec(dllexport) void CreateRect(sf::Vertex &vert);
}      

 

gameCode.cpp:
#include "gameCode.h"

__declspec(dllexport) void CreateRect(sf::RectangleShape RS)
{
        RS.setSize(sf::Vector2f(2.0f, 2.0f));
       
}


 

build.bat
@echo off

:: IMPORTANT!  "=" has to come IMMEDIATLEY after VARIABLE NAME

SET compilerSwitches= /MD /EHsc -Zi -nologo
SET winLibs= user32.lib gdi32.lib winmm.lib Opengl32.lib ws2_32.lib advapi32.lib

SET sfmlDependLibsX64= /LIBPATH:"C:\SFML\SFML-2.5.1_x64\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib
SET sfmlDependLibsX86= /LIBPATH:"C:\SFML\SFML-2.5.1_x86\lib" freetype.lib openal32.lib flac.lib vorbisenc.lib vorbisfile.lib vorbis.lib ogg.lib

SET sfmlLibsX64= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib
SET sfmlLibsX86= sfml-system-s.lib sfml-graphics-s.lib sfml-window-s.lib sfml-audio-s.lib sfml-network-s.lib

If NOT EXIST ..\buildx64 mkdir ..\buildx64
If NOT EXIST ..\buildx86 mkdir ..\buildx86
If NOT EXIST ..\assets mkdir ..\assets

 pushd ..\buildx64
 
REM cl %compilerSwitches% /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 cl %compilerSwitches% /I ..\include ..\src\gameCode.cpp /LD %winLibs% /link %sfmlDependLibsX64% %sfmlLibsX64%
 
 popd
 
 @echo x64 build Done!
 
 ::------------------ x86 BUILD (set vcvarsx86) -----------------

 ::pushd ..\buildx86
 
 ::cl /MD /EHsc -Zi -nologo /I ..\include ..\src\CJsfml_002.cpp %winLibs% /link %sfmlDependLibsX86% %sfmlLibsX86%
 
 ::popd
 ::
 ::@echo x86 build Done!

del *.~ *.cpp~ *.bat~ *.un~ *.h~

 

compilation Errors:
Code: [Select]
C:\SFML_Projects\SFML_CONSOLE_DEV\CJsfml_002\src>build
gameCode.cpp
   Creating library gameCode.lib and object gameCode.exp
gameCode.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl sf::RectangleShape::setSize(class sf::Vector2<float> const &)" (__imp_?setSize@RectangleShape@sf@@QEAAXAEBV?$Vector2@M@2@@Z) referenced in function CreateRect
gameCode.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl sf::RectangleShape::~RectangleShape(void)" (__imp_??1RectangleShape@sf@@UEAA@XZ) referenced in function CreateRect

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trying to manage Hot loading code with SFML
« Reply #1 on: August 12, 2019, 06:38:33 am »
If you link to static SFML libraries, you must define the SML_STATIC preprocessor symbol, as stated in the tutorial.
Laurent Gomila - SFML developer

C_Worm

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Trying to manage Hot loading code with SFML
« Reply #2 on: August 12, 2019, 08:42:26 am »
If you link to static SFML libraries, you must define the SML_STATIC preprocessor symbol, as stated in the tutorial.

Ohooo, lul that was it!

however when i use the extern "C" i get a warning

Code: [Select]
gameCode.h(6): warning C4190: 'CreateRect' has C-linkage specified, but returns UDT 'sf::RectangleShape' which is incompatible with C


is this something that one would have to worry about or can i ignore it?

i need to use extern "C" so that i can find the functions later and store it in a function pointer.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Trying to manage Hot loading code with SFML
« Reply #3 on: August 12, 2019, 03:58:48 pm »
You declare the function as being C, but its prototype contains C++ symbols that a C compiler can't understand. A simple workaround is to make typedefs for those types (eg. using sfRectangleShape = sf::RectangleShape;).
Laurent Gomila - SFML developer