C2059 syntax error 'string' ? - c++

extern "C"
{
#endif
#include <stdint.h>
#include <limits.h>
#include "attributes.h"
}
#endif
I added extern "C" { }
Then i got the C2059 string error
So i tried to use #endif, now i have another 4 errors.
Error 1 error C2059: syntax error : 'string' d:\c-sharp\c++
compiling\consoleapplication7\consoleapplication7\libavutil\rational.h 31 1
ConsoleApplication7
How can i fix this string error ?

At a guess, are you including this code from a C source file?
extern "C" { guards are only required (or understood) by C++. You can omit them from a C file, should include them in a C++ file and should guard them with a __cplusplus ifdef in a header file.
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <limits.h>
#include "attributes.h"
#ifdef __cplusplus
}
#endif

Related

LNK2005 & LNK1169 with extern function

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

‘_snprintf’ was not declared in this scope

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

C and C++ --- undefined reference to

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.

Ffmpeg headers do not compile in windows

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.

C++ lnk2005 error on two files with extern "C", why?

I have a CPP with extern "C" functions. If they are all in a single file, everything works great. I want to split up the functions into different files just for organizational purpose.
So lets say I have these two files:
File_One.cpp
#pragma once
#include "stdafx.h"
#include <windows.h>
#include "Functions.h"
#include "Variables.h"
#include <string>
#include "File_Two.cpp"
extern "C"
{
__declspec(dllexport) void MethodOne()
{
MethodTwo();
}
}
File_Two.cpp
#pragma once
#include "stdafx.h"
#include <windows.h>
#include "Functions.h"
#include "Variables.h"
#include <string>
extern "C"
{
__declspec(dllexport) void MethodTwo()
{
}
}
I have tried rearranging my include headers in different order, and even place no include headers in file_one.cpp other than the include for file_two.cpp but I always get the same errors.
1) error LNK1169: one or more multiply defined symbols found
2) error LNK2005: _MethodTwo already defined in File_One.obj
What exactly am I doing wrong?
What should I do to fix it?
Thank you!
You're probably running into issues because you're including the File_two.cpp file in your File_one.cpp file. What is happening is that File_two.cpp and File_one.cpp are getting compiled and linked. But because File_two.cpp is included in File_one.cpp, the linker is seeing two copies of MethodTwo, and can't decide which to use.
You should move the declarations to a header:
File_two.h:
extern "C"
{
__declspec(dllexport) void MethodOne()
}
And include that instead.
File_one.h:
extern "C"
{
__declspec(dllexport) void MethodOne();
}
Then define the functions with their body in their respective .cpp files. No need for extern "C" in the source files.