obsproject / obs-plugintemplate

GNU General Public License v2.0
302 stars 138 forks source link

error LNK2019 when using `obs_frontend_*` functions #61

Closed Susko3 closed 1 year ago

Susko3 commented 1 year ago

Operating System Info

Windows 10

Other OS

No response

OBS Studio Version

Other

OBS Studio Version (Other)

plugin-deps-2022-08-02-qt6-x64

OBS Studio Log URL

-

OBS Studio Crash Log URL

No response

Expected Behavior

I would expect the template to build fine when using functions from obs-frontend-api.h.

Current Behavior

The build fails with:

MSBuild version 17.4.1+9a89d02ff for .NET Framework
...
plugin-main.obj : error LNK2019: unresolved external symbol obs_frontend_add_event_callback referenced in function obs_module_load [...\obs-plugintemplate\build_x64\obs-plugintemplate.vcxproj]
plugin-main.obj : error LNK2019: unresolved external symbol obs_frontend_remove_event_callback referenced in function obs_module_unload [...\obs-plugintemplate\build_x64\obs-plugintemplate.vcxproj]
...\obs-plugintemplate\build_x64\RelWithDebInfo\obs-plugintemplate.dll : fatal error LNK1120: 2 unresolved externals [...\obs-plugintemplate\build_x64\obs-plugintemplate.vcxproj]

Steps to Reproduce

  1. Clone this repo
  2. Modify plugin-main.c like so, just adding a simple obs frontend event callback:
    
    #include <obs-module.h>
    #include <obs-frontend-api.h>

include "plugin-macros.generated.h"

void callback(enum obs_frontend_event event, void* data) { (void)data;

switch(event)
{
    case OBS_FRONTEND_EVENT_STREAMING_STARTED:
        blog(LOG_INFO, "stream started");
        break;

    case OBS_FRONTEND_EVENT_STREAMING_STOPPED:
        blog(LOG_INFO, "stream stopped");
        break;
}

}

OBS_DECLARE_MODULE() OBS_MODULE_USE_DEFAULT_LOCALE(PLUGIN_NAME, "en-US")

bool obs_module_load(void) { blog(LOG_INFO, "plugin loaded successfully (version %s)", PLUGIN_VERSION);

obs_frontend_add_event_callback(callback, 0);
return true;

}

void obs_module_unload() { obs_frontend_remove_event_callback(callback, 0);

blog(LOG_INFO, "plugin unloaded");

}

3. Build the project using `.\.github\scripts\Build-Windows.ps1`

### Anything else we should know?

The following diff fixes the problem. This just moves the `obs-frontend-api` lookup below some of the other directives. Not sure why it works.

```diff
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3b060d6..2ba1e8a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -26,8 +26,6 @@ include(cmake/ObsPluginHelpers.cmake)

 # Uncomment these lines if you want to use the OBS Frontend API in your plugin
 #[[
-find_package(obs-frontend-api REQUIRED)
-target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OBS::obs-frontend-api)
 #]]

 # Uncomment those lines if you want to use Qt in your plugin
@@ -45,6 +43,9 @@ configure_file(src/plugin-macros.h.in ${CMAKE_SOURCE_DIR}/src/plugin-macros.gene

 target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/plugin-macros.generated.h)

+find_package(obs-frontend-api REQUIRED)
+target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OBS::obs-frontend-api)
+
 # /!\ TAKE NOTE: No need to edit things past this point /!\

 # --- Platform-independent build settings ---
glikely commented 1 year ago

This isn't a bug. The obs-frontend-api block is specifically commented out in the CMakeLists.txt file, and it even tells you how to enable it:

# Uncomment these lines if you want to use the OBS Frontend API in your plugin
#[[
...
#]]

You're supposed to remove the #[[ and #]] lines to enable linking your plugin against obs-frontend-api.

Susko3 commented 1 year ago

You're supposed to remove the #[[ and #]] lines to enable linking your plugin against obs-frontend-api.

Does it work for you? To me it gives a CMake syntax parse error. I think those are just markers to the reader to know what "these lines" refer to.

    =>   Configuring obs-plugintemplate...
CMake Error at CMakeLists.txt:31:
  Parse error.  Expected a command name, got bracket argument with text
  "find_package(obs-frontend-api REQUIRED)

  target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OBS::obs-frontend-api)

  ".

-- Configuring incomplete, errors occurred!
Exception: C:\Users\Susko3\code\git\obs-plugintemplate\.github\scripts\Build-Windows.ps1:101
Line |
 101 |  Build
     |  ~~~~~
     | cmake -S . -B build_x64 -G Visual Studio 17 2022 -DCMAKE_SYSTEM_VERSION=10.0.20348.0 -DCMAKE_GENERATOR_PLATFORM=x64 -DCMAKE_BUILD_TYPE=RelWithDebInfo
     | -DCMAKE_PREFIX_PATH:PATH=C:\Users\Susko3\code\git\obs-build-dependencies\plugin-deps-2022-08-02-qt6-x64 -DQT_VERSION=6 exited with non-zero code 1.
RytoEX commented 1 year ago

You're supposed to remove the #[[ and #]] lines to enable linking your plugin against obs-frontend-api.

Does it work for you? To me it gives a CMake syntax parse error. I think those are just markers to the reader to know what "these lines" refer to.

If you are trying to use the obs-frontend-api, you are absolutely supposed to remove #[[ and #]], which are CMake comment blocks. This is mentioned in the comment above them, which states:

# Uncomment these lines if you want to use the OBS Frontend API in your plugin
Susko3 commented 1 year ago

you are absolutely supposed to remove #[[ and #]], which are CMake comment blocks.

Okay that makes sense, my editor doesn't properly support them so I was confused about it. Works wonders now, thanks!