Jared
Jared

Reputation: 1469

MSVC fatal error LNK1120: 1 unresolved externals with FFMPEG libs

I am trying to utilize the ffmpeg libraries in a program of my own and am having trouble linking them. Specifically, In my a basic program I am receiving fatal error LNK1120: 1 unresolved externals errors. The program is:

#include <iostream>
#include <libswresample/swresample.h>

int main()
{
    std::cout << "Hello World!\n";
    struct SwrContext* swr_ctx = swr_alloc();
    if (!swr_ctx) {
        std::cout << "Could not allocate resampler context";
    }
}

I downloaded prebuild libraries from https://ffmpeg.zeranoe.com/builds/, specifically the Windows x64 dev package which includes the .def/.lib as well as .dll files.

I originally tried (and intend to ultimately use) cmake to generate the MSVC sln files. The cmake file is:

cmake_minimum_required(VERSION 3.5)

project(ffmpeg_jni)

# Find the JNI bits
find_package(JNI)

# Search for the ffmpeg libraries
set(ffmpeg_include_hint "ffmpeg-dev/include")
set(ffmpeg_lib_hint "ffmpeg-dev/lib")

find_path(SWRESAMPLE_INCLUDE_DIR libswresample/swresample.h PATHS ${ffmpeg_include_hint})
find_library(SWRESAMPLE_LIBRARY swresample PATHS ${ffmpeg_lib_hint})
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES
    IMPORTED_LOCATION "${SWRESAMPLE_LIBRARY}"
    IMPORTED_IMPLIB "${SWRESAMPLE_LIBRARY}"
    INTERFACE_INCLUDE_DIRECTORIES "${SWRESAMPLE_INCLUDE_DIR}"
)

# Setup basic include dirs
set(includeDIRS
        src/main/cpp
        ${JAVA_INCLUDE_PATH})

# Setup windows specific includes
set(includeDIRS
        ${includeDIRS}
        ${JAVA_INCLUDE_PATH}/Win32)

include_directories(${includeDIRS})

set(WRAPPER_SRC
        src/main/cpp/logging.c
        src/main/cpp/logging.h
        src/main/cpp/main.cpp)

add_library(ffmpeg_jni SHARED ${WRAPPER_SRC})
target_link_libraries(ffmpeg_jni PRIVATE swresample)

The generated solution compiles and has proper access to the include files (Visual Studio can even take me to the declarations). The issue comes in the linking phase of the build where I receive:

error LNK2019: unresolved external symbol "struct SwrContext * __cdecl swr_alloc(void)" (?swr_alloc@@YAPEAUSwrContext@@XZ) referenced in function main

Thinking that I perhaps had something wrong in cmake since I am still pretty new with it I tried making a simple demo as a pure visual studio project following what I have found in countless online demos for adding an external library to a project. Specifically this included:

At this point any searching efforts I undertake show me different people doing the same things which tells me I've been looking at this too long to find the answer myself and its something trivial that I'm missing/not understanding.

Upvotes: 0

Views: 759

Answers (1)

Vencat
Vencat

Reputation: 1622

If you are compiling your project as c++, then include FFmpeg headers as below which tells your c++ compiler to handle FFmpeg headers as C instead of C++.

extern "C" {
#include <libswresample/swresample.h>
}

Upvotes: 2

Related Questions