Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error C2059: syntax error : 'string'

I have looked at other posts and to be honest I am still not sure what is causing the problem. I am programming in Visual Studio and

I have the following code: (this is a C main)

int main(int arc, char **argv) {
       struct map mac_ip;
       char line[MAX_LINE_LEN];

       char *arp_cache = (char*) calloc(20, sizeof(char));   //yes i know the size is wrong - to be changed
       char *mac_address = (char*) calloc(17, sizeof(char));
       char *ip_address = (char*) calloc(15, sizeof(char));

       arp_cache = exec("arp -a", arp_cache);

It uses the following cpp code:

#include "arp_piping.h"

extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe) {
    pipe = _popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL) {
              strcat(arp_cache, buffer);
        }
    }
    _pclose(pipe);
    return arp_cache;
}

With the matching header file:

#ifndef ARP_PIPING_H
#define ARP_PIPING_H
#endif

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

#include <stdio.h>
#include <string.h>

extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe);

#undef EXTERNC

But I keep on getting the following errors:

1>d:\arp_proto\arp_proto\arp_piping.h(14): error C2059: syntax error : 'string'
1>main.c(22): warning C4013: 'exec' undefined; assuming extern returning int
1>main.c(22): warning C4047: '=' : 'char *' differs in levels of indirection from 'int'

Please can I get some help, I have looked at other posts regarding the c2059 but am still getting nowhere

like image 426
cxzp Avatar asked Sep 17 '25 02:09

cxzp


2 Answers

Change your exec declaration to use the EXTERNC macro you have taken pains to define.

EXTERNC char *exec(char* cmd, char* arp_cache, FILE* pipe);
like image 104
jxh Avatar answered Sep 19 '25 17:09

jxh


I ran into this compilation error when adding an enum to a project. It turned out that one of the values in the enum definition had a name clash with a preprocessor #define.

The enum looked something like the following:


// my_header.h

enum Type 
{
   kUnknown,
   kValue1,
   kValue2
};

And then elsewhere there was a #define with the following:


// ancient_header.h

#define kUnknown L"Unknown"

Then, in a .cpp somewhere else in the project, both of these headers were included:


// some_file.cpp

#include "ancient_header.h"
#include "my_header.h"

// other code below...


Since the name kUnknown was already #define'd, when the compiler came to the kUnknown symbol in my enum, it generated an error since the symbol was already used to define a string. This caused the cryptic syntax error: 'string' that I saw.

This was incredibly confusing since everything appears to be correct in the enum definition and compiles just fine on it's own.

It didn't help that this was in a very large C++ project, and that the #define was being transitively included in a completely separate compilation unit and was written by someone 15 years ago.

Obviously, the right thing to do from here is rename that terrible #define to something less common than kUnknown, but until then, just renaming the enum value to something else works as a fix, e.g.:


// my_header.h

enum Type 
{
   kSomeOtherSymbolThatIsntDefined,
   kValue1,
   kValue2
};

Anyway, hopefully this answer is helpful for someone else, since the cause of this error stumped me for a good day and a half.

like image 34
pje Avatar answered Sep 19 '25 16:09

pje