MSVC fatal error LNK1120: 1 unresolved externals with FFMPEG libs - visual-studio

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:
Adding the directory containing the header files to Properties->C/C++->General->Additional Include Directories
Adding the directory containing the .lib files to Properties->Linker->General->Additional Library Directories (Note that the cmake path did not do this but instead added the lib file via a relative path)
Adding the .lib file to Properties->Linker->Input->Additional Dependencies
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.

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>
}

Related

How to build additional caffe tools?

Under tools folder caffe library have some tools as single .cpp files
https://github.com/BVLC/caffe/tree/master/tools
I have added my own tool under this folder and can build it via cmake.
The problem that when I have added additional dependency (json lib jsoncpp) build fails at linking phase.
I have put json lib .h, .cpp files under tools/json folder.
My includes:
#include <iostream>
#include "opencv2/opencv.hpp"
#include "caffe/caffe.hpp"
#include "json/json.h"
Error that I get, like:
Undefined symbols for architecture x86_64:
"Json::StyledWriter::write(Json::Value const&)", referenced from:
image_list_processing(int, char**) in my_tool.cpp.o
So the question is how to add lib to linking process? Should I modify https://github.com/BVLC/caffe/blob/master/tools/CMakeLists.txt or maybe I should add separate file under tools/json/CMakeLists.txt?
This is not an error of missing .h file. You need to link your code to the shared object (libjsoncpp.so of similar) for your code to access (link) compiled json functions.
add -L /path/to/libjson_folder and -ljsoncpp flags to the linking stage of your makefile.

cmake generated visual studio solution failed to build

I am currently working on a project on windows 10 and was asked to use some c++ code for it. The code was written with plattform independence in mind. So I used the given cmake files to build a visual studio solution (VS 2015 Update 3). But when I try to build the solution it gives me over 900 errors and warnings.
The same project was successfully build on a mac using cmake and g++. The project itself uses a Qt gui and some additional libaries like boost.
To understand the errors you have to know, that the solution consists of 5 projects: ALL_BUILD, iModControll, iModDaemon, iModView and ZERO_CHECK
There are a lot errors like:
missing type specifier - int assumed. Note: C++ does not support default-int
inconsistent dll binding
declaration not found
unexpected token before ;
and so on...
You can see the full list here
I am very new to cmake and not an VS expert, so please help me to understand what went wrong here. Again: the code compiled perfectly under OSX.
EDIT:
I used mingw, as #MaxGo suggested. The errors and warnings were reduced after that. Here is what I have now:
warning: void DC_TermFunc()' redeclared without dllimport attribute after being referenced with dll linkage
void DC_TermFunc(void)
{
if(controller)
{
qDebug() << "Terminating iModSkynet";
runthread->terminate();
delete runthread; // This will delete the controller too
}
}
error: function 'void DC_SendData(char, char*, int)' definition is marked dllimport
IMODCONTROLLSHARED_EXPORT void DC_SendData(char oc, char* data, int length)
{
QByteArray qdata = QByteArray(data, length);
controller->sendBlenderCommunication(oc, qdata);
}
The other errors are similar. My question is: Are there any known issues when porting c++ source code to windows, why does it not show as many errors as in VS and what can I do to solve these errors?
CMake Configuration- Boost- Visual Studio - C++
Step 1: Download CMake installation file, install and save it in local disk
Step 2: Create 2 Folders in local disk a) Raw File b)Solution File
Step 3: Raw File folder- Create a Main.cpp file and paste your raw c++ code it it and save. Now in the same folder create a txt file named CMakeLists and paste the following code in it and save.
cmake_minimum_required(VERSION 3.7)
project (cmboosttest)
#find_package(Boost REQUIRED)
#include_directories(${Boost_INCLUDE_DIR})
add_executable(boosttest ${PROJECT_SOURCE_DIR}/Main.cpp)
Step 4: Open CMake choose Raw File folder in browse source and Solution File Folder in browse build.
Step 5: Click Configure. Once it succeeds, click Generate.
Step 6: When you open the Solution File folder, you can see the built solution file. Click on it and your program will open in Visual Studio. Now, build your program in VS.
Hope this helps!

LINK error in visual studio with boost date_time library

I built date_time library only and set up property page like this:
linker > input > additional dependencies:
libboost_date_time-vc120-mt-gd-1_55.lib boost_date_time-vc120-mt-gd-1_55.lib;%(AdditionalDependencies)
VC++ directories > library directories:
C:\boost_1_55_0\stage\lib;$(LibraryPath)
VC++ directories > include directories:
C:\boost_1_55_0;$(IncludePath)
when runing build I got following error:
1>LINK : fatal error LNK1104: cannot open file
'boost_date_time-vc120-mt-gd-1_55.lib.obj'
linker can't find the *.obj file, I can't find it either, where do need to look for *.obj file and what to do once I find it?
You shouldn't specify additional dependencies. Boost will auto link libraries using #pragma comment(lib, "<lib name>") when you include their headers. Try removing those explicit dependencies and rebuild.

How can I find out which library is including libcmt?

I'm trying to link a Windows executable that depends on a several static libraries (some of which I have built, some of which I have not). When I do the link, I get a flock of errors like:
LIBCMT.lib(mlock.obj) : error LNK2005: _unlock already defined in MSVCRT.lib(MSVCR100.dll)
which is the classic /MD vs. /MT problem (whether the C runtime is statically or dynamically linked). I tried the easy solution first, adding the linker flags
/nodefaultlib:libcmt /nodefaultlib:libcpmt
but that just gave different errors:
msvcprt.lib(MSVCP100.dll) : error LNK2005: "public: __cdecl std::_Locinfo::~_Locinfo(void)" (??1_Locinfo#std##QEAA#XZ) already defined in gtest.lib(gtest-all.cc.obj)
gtest.lib(gtest-all.cc.obj) : error LNK2001: unresolved external symbol "private: static int std::locale::id::_Id_cnt" (?_Id_cnt#id#locale#std##0HA)
I've gone through the libraries I'm building, and as far as I can tell I'm building them all /MD. I say "as far as I can tell" because some of them are third-party libraries that come with their own makefiles so I don't have complete control over the build process..
I don't think "depends" works on libraries (only EXEs and DLLs), is there a tool that would let me look at the various libraries I'm linking in, and see which one is bringing in libcmt when I want to be using msvcrt instead?
I have had the same problem and I used dumpbin ( http://msdn.microsoft.com/en-us/library/z66yd3h6.aspx ) with /DIRECTIVES options on the libs. It will show a number of /DEFAULTLIB sections, each one is another lib that your lib try to use. Dumpbin needs to run from the Visual Studio command promt.
dumpbin /DIRECTIVES liblua52.lib
I had 100+ libs with all the solution configurations and platforms so I made a python 2.7 script (crtdisplay.py) to do it for me. It can be found on my bitbucket repository at https://bitbucket.org/vimarina/ctrlcv/src/57b7ddca15b5c7fefddcf20ffcea0633223a4bd6/crtdisplay . Put it in the root directory of your libs. Not much error checking in that code so do not be surprised if it fails :). I used Visual Studio 2010 so might fail on other versions of Visual Studio.
crtdisplay.py > info.txt

Q_OBJECT Problem in Visual C++

I have received a Visual C++ QT based project from our customer. I have installed QT libraries then I have compiled the project. The Project was compiled without any problems.
Now, I need to include a new additional GUI interface with the existing project. I have created a GUI in QT designer then saved in the source directory of VC++ project. Then I have written .h and .cpp file for new GUI and could call this interface. Now I need to include SIGNALS and SLOTS, when I include Q_OBJECT in .h file. I have compilation error.
These are the errors, please help me to solve this problem:
unresolved external symbol "public: virtual struct QMetaObject const * __thiscall BetaLineServer::metaObject(void)const " (?metaObject)
unresolved external symbol "public: virtual void * __thiscall BetaLineServer::qt_metacast(char const *)"
unresolved external symbol "public: virtual int __thiscall BetaLineServer::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall#
Any time you add a Q_OBJECT macro to a class you need to be sure to rerun qmake and then compile.
All you need is to compile the header files with moc that contain Q_OBJECT macro. And how to do that? here it is
You can manually type commands for compiling moc or uic files OR
-> Install Qt-VS addin. http://qt.nokia.com/downloads/visual-studio-add-in
-> Now, open visual studio and create a new Qt project as described in here and then
-> Right click on a header file that contains the Q_OBJECT macro you should find something like below. Copy these commands into your project.
-> Replace the header file name with yours in the 'Command Line' command
-> compile once and that should generate moc_xxxxx.cpp files, include them in your project.
For future reference, if you create a Qt project in VS using this plugin you should have these commands automatically added
You probably just need to add your .ui, .cpp and .h files to the project file.
Qt will run 'moc' on the ui file if it's listed there, which creates source code to supply your missing symbols.
Exclude .h file from the project and include it again - moc_.cpp will appear in the "Generated Files", and linking errors are disapeared.

Resources