Sprotte
Sprotte

Reputation: 191

Naming conflict between c library and c++ library

I do not normally program in C++ and I am more comfortable in C. I have a C library that I wrote and now I am trying to use it together with a specific function (a graph planarity testing function) in the Boost C++ Graph library. In so doing, I am getting a compilation error which seems to involve a naming conflict between my part of my own c libraries linked list implementation and some far away link function in some directory in the bowls of my computer that is being called on by one of these Boost library functions:

    ./list.c:7:22: error: redefinition of 'link' as different kind of symbol
typedef struct node *link;
                     ^
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:464:6: note: previous definition is here
int      link(const char *, const char *);
         ^
In file included from boost_graph_test.cpp:15:
In file included from ./graph_dt.c:1:

How should I go about fixing this naming conflict (I would rather not change the code in my existing c library)? I feel like some namespace thing should do the trick but unfortunately, I am not sure what exactly to do.

Upvotes: 1

Views: 210

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12668

Well:

typedef struct node *link;

/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/unistd.h:464:6: note: previous definition is here
  • Here you define link as a type.
int      link(const char *, const char *);
         ^
In file included from boost_graph_test.cpp:15:
In file included from ./graph_dt.c:1:
  • Here you are defining (probably not you, the system has a function called link to make a second link of a file.

You cannot have a type and a function with the same name. The solution IMHO is to rename your type to something like

typedef struct node *link_t;

and Voila!!!

(Note: you will have to search for all occurences of type link in your sources and rename them also to link_t)

Upvotes: 1

dbush
dbush

Reputation: 223972

The link function is a standard system function used for creating hard links and symbolic links in the filesystem. It's declared in unistd.h (which is used pretty commonly) and has been around since 4.3 BSD. So you can't use this name for something else.

This means that, given this this is a C library, you'll have to rename your variable.

Upvotes: 3

Related Questions