I try to have a simple function which returns the current directory as a string:
Utils.h contains :
#ifndef __UTILS__
#define __UTILS__
#pragma once
#include <string>
#include <stdio.h> /* defines FILENAME_MAX */
#ifdef __linux__
#include <unistd.h>
#define GetCurrentDir getcwd
#else
#include <direct.h>
#define GetCurrentDir _getcwd
#endif
namespace
{
extern std::string _getCurrentDirectory()
{
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
return "";
cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
return std::string(cCurrentPath);
}
}
#endif // __UTILS__
Despite extern declaration (redundant ?) and the creation of this specified Utils.h file, I still have linking problem:
Error 2 error LNK2005: "char * __cdecl getCurrentDirectory(void)" (?getCurrentDirectory##YAPADXZ) already defined in MyLibrary.lib(myobj.obj)
Error 3 error LNK1169: one or more multiply defined symbols found
This problem appears twice, and MyLibrary.lib doesn't even use or includes Utils.h
I really don't understand despite of the time I spent on it.
Does anyone have an idea ?
Thanks
Related
I have a dll and header file. Now I am creating a console application using vs2015. and load this library. But while getting GetProcAddress of function. It is always returning NULL. Here is my code.
Contents of header file (test.h) (This header file is only for your reference)
#ifdef APPLYDLL_EXPORTS
#define APPLYDLL_API __declspec(dllexport)
#else
#ifdef BUILD_EXE
#define APPLYDLL_API
#else
#define APPLYDLL_API __declspec(dllimport)
#endif
#endif
#include <string>
#include <winerror.h>
APPLYDLL_API HRESULT ApplySettings(std::string input);
Contents of console application (cpp file)
#include "stdafx.h"
#include <Windows.h>
#include <string>
using namespace std;
//Define the function prototype
typedef HRESULT(CALLBACK* ApplySettings)(std::string);
int main()
{
HINSTANCE hLib = LoadLibrary(TEXT("ApplyTool.dll"));
if (NULL != hLib)
{
//Get pointer to our function using GetProcAddress:
ApplySettings applySettings = (ApplySettings)GetProcAddress(hLib,"ApplySettings");
DWORD errorcode=GetLastError(); //errorcode 127, Procedure not Found
}
return 0;
}
I don't know, Where I am doing silly mistake.
Someone please help me.
Thanks in adv.
I Agree with CrazyCoder, check the DLL using Dependency Walker with that function name.
and try to change define as like below,
#define APPLYDLL_API __declspec(dllexport)
to
#define APPLYDLL_API extern "C" __declspec( dllexport )
Thanks, Jake.
I am working on a ffmepg c++ project which links a hpp file, in the hpp file:
#define snprintf _snprintf
#include <stdio.h>
#include <cstdio>
#include <unistd.h>
include <assert.h>
#if defined _MSC_VER && _MSC_VER >= 1200
#pragma warning( disable: 4244 4510 4512 4610 4146 4996 4005)
#define sprintf sprintf_s
#define _sprintf _sprintf_s
#define _snprintf _snprintf_s
#endif
snprintf (oc->filename, sizeof(oc->filename), "%s", filename);
it gives the error:
‘_snprintf’ was not declared in this scope
It is quite weird the error shows ‘_snprintf’ while what I use is 'snprint'. This code is wrote by others, I did not understand these #define he used. If i remove the line #define sprintf sprintf_s, it gives error:
segmentation fault(core dumped)
Due to ffmpeg is incompatible with C++, I have include the stdio.h and cstdio both within extern C and out of extern C, but the error continue show out. What is the problem? How to fix it?
At first change this line
include <assert.h>
with:
#include <assert.h>
and write this line
#define snprintf _snprintf
after includes and try compiling code
i need help!
I've implemented a .cpp program in the next path: home/virginia/android/vlc/src/input/virtual.cpp
/*
* virtual.cpp
*/
#include <stdlib.h>
#include <jni.h>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui_c.h>
#include <opencv2/photo/photo.hpp>
#include <android/log.h>
extern "C" {
#include "virtual.h"
}
#define LOG_TAG "VLC - Imagen 3D - JNI"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
using namespace cv;
using namespace std;
extern "C" int camino(void){... }
This programs calls to the opencv functions.
virtual.h ( home/virginia/android/vlc/src/input/virtual.h)
/*
* virtual.h
*/
#ifndef __VIRTUAL_H
#define __VIRTUAL_H
int camino(void);
#endif /*__VIRTUAL_H*/
But the problem is that i need to call to "camino" function from decoder.c (( home/virginia/android/vlc/src/input/decoder.c):
#include "virtual.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <assert.h>
...
static void DecoderPlayVideo( decoder_t *p_dec, picture_t *p_picture,
int *pi_played_sum, int *pi_lost_sum ){
...
int res = camino();
msg_Warn( p_dec, "Llamada a virtualJNI devuelve %d", &res );
vout_PutPicture( p_vout, p_picture );
...
}
...
I'm getting the next error:
/home/virginia/android-ndk-r8c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ../vlc/android/src/.libs/libvlccore.a(decoder.o): in function DecoderDecodeVideo:../../src/input/decoder.c:1501: error: undefined reference to 'camino'
collect2: ld returned 1 exit status
make[1]: *** [obj/local/armeabi-v7a/libvlcjni.so] Error 1
make[1]: Leaving directory `/home/virginia/android/vlc-android'
make: *** [vlc-android/obj/local/armeabi-v7a/libvlcjni.so] Error 2
What am i doing wrong? Thanks so much
Regarding the extern "C" stuff, it's not recommended to do this:
extern "C" {
#include "virtual.h"
}
Instead, you should put it in the header:
/*
* virtual.h
*/
#ifndef __VIRTUAL_H
#define __VIRTUAL_H
#ifdef __cplusplus
extern "C" {
#endif
int camino();
#ifdef __cplusplus
}
#endif
#endif /*__VIRTUAL_H*/
You'll see this in almost all headers that are meant to be shared between C and C++.
As for your real problem, you'll have to show us the actual linker command, not just the error, but it looks like virtual.o isn't being linked into your program, and possibly isn't being built.
I am trying to link to ffmpeg under windows, but have run into difficulties. Inclusion of ffmpeg headers causes hundreds of compilation errors which i can't easily fix.
1) timestamp.h uses snprintf instead of _snprintf. I have tried to add it as a macro, like this:
#ifdef Q_OS_WIN
#define snprintf _snprintf
#endif
#define __STDC_CONSTANT_MACROS
namespace ffmpeg {
extern "C" {
#include <libavutil/imgutils.h>
#include <libavutil/samplefmt.h>
#include <libavutil/timestamp.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
} }
but it didn't help. Why would the macro not propagate inside?
2) There again, PRId64 isn't defined. I have defined __STDC_CONSTANT_MACROS before inclusion of timestamp.h, but definition isn't retrieved from inttypes.h
In compiler output, it looks like:
ffmpeg\include\libavutil/timestamp.h(42) : error C3861: 'snprintf': identifier not found
ffmpeg\include\libavutil/timestamp.h(42) : error C2146: syntax error : missing ')' before
identifier 'PRId64'
You did include them into a extern "C" right?
extern "C" {
#include <libavutil/imgutils.h>
#include <libavcodec/avcodec.h>
#include <libswscale/swscale.h>
}
Is this a typo? You talk about "snprintf" ("n", not "m").
#define smprintf _snprintf
Even then, timestamp.h probably redifines it or includes something that does.
I'm currently using the triangle library in my program. The library contains only .c and .h files (no .lib). I get the following error on Visual Studio C++ 2010:
1>data.obj : error LNK2019: unresolved external symbol _triangulate referenced in function "struct triangulateio __cdecl readfile(void)" (?readfile##YA?AUtriangulateio##XZ)
The header file of my data.cpp is the following:
#ifndef DATA_H
#define DATA_H
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <time.h>
#include <GL/gl.h> //include the gl header file
#include <GL/glut.h> //include the glut header file
#include <GL/glu.h> //include the glut header file
#include <armadillo>
//Namespace
using namespace std;
using namespace arma;
extern "C"
{
#ifdef SINGLE
#define REAL float
#else /* not SINGLE */
#define REAL double
#endif /* not SINGLE */
#include "triangle.h"
}
triangulateio readfile();
#endif
Data.cpp
triangulate("pczAevn", &in, &mid, &vorout);
I've already made my program work with a Makefile of mine on Ubuntu, but I need to run my program on windows.
Feel free to ask for more information.
EDIT #1:
If you use the triangle library with VS, you have to put the following instruction on top of the triangle.c file #define TRILIBRARY
Now it compile. Thank you very much for the help.
The linker can't find a definition for "triangulateio readfile()", if it's defined in the .c file my guess is that it isn't built. If you include it in the project it could work.