I have a C++ program called myArchive that calls routines from a static C library.
My code for myArchive has a class called Archive that contains a private variable called md, which of type Metadata *, defined in my C library.
Here’s the myArchive.hpp header:
#ifndef MYARCHIVE_H
#define MYARCHIVE_H
#include "myLibraryHeaders.h"
...
namespace Archive {
class Archive {
public:
Archive();
virtual ~Archive();
Metadata * getMd() { return md; }
Metadata ** getMdRef() { return &md; }
void setMd(Metadata *_md) { md = _md; }
private:
Metadata *md;
};
Archive::Archive() {
md = NULL;
}
Archive::~Archive() {
if (md != NULL)
freeMetadata(&md);
}
}
#endif
The freeMetadata() function is just releasing items in a linked list:
void freeMetadata(Metadata **md) {
Metadata *iter;
Metadata *prev = NULL;
if (! *md)
return;
for (iter = *md; iter != NULL; iter = iter->next) {
/* ... */
if (prev != NULL)
free(prev);
prev = iter;
}
if (prev != NULL) {
free(prev);
prev = NULL;
}
}
Here is the declaration in one of the headers referenced in myLibraryHeaders.h:
#ifdef __cplusplus
extern "C" {
#endif
#ifndef METADATAHELPERS_H
#define METADATAHELPERS_H
typedef struct metadata {
/* ... */
struct metadata *next;
} Metadata;
/* ... */
void freeMetadata(Metadata **md);
/* ... */
#endif
#ifdef __cplusplus
}
#endif
I have some other variables, but this is the stuff relevant to my question, which is:
When I try to compile this, I get an out-of-scope error:
...
g++ -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE=1 -DUSE_ZLIB -DUSE_BZLIB -O3 -Wformat -Wall -Wswitch-enum -static -c myArchive.cpp -o myArchive.o
myArchive.hpp: In destructor "virtual myArchive::Archive::~Archive()":
myArchive.hpp:87: error: "freeMetadata" was not declared in this scope
make: *** [myArchive] Error 1
But I am including a header file (in myLibraryHeaders.h) that declares freeMetadata(). Further, the compiler isn’t complaining about the Metadata type, which itself is also defined in one of the headers in myLibraryHeaders.h.
What am I missing or doing wrong?
The only explanation that makes sense is that you’re somehow getting
METADATAHELPERS_Hdefined prior to the include of myLibraryHelpers.h, probably due to some prior include somewhere — some kind of circular include dependency. You can try usingg++ -Eto extract the post-preproceesed text and try to see if you can untangle the problem from that (search for where freeMetaData appears in that to get some idea of the order that you’re ending up with), but in general solving these problems can be tricky