Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using standard function name in C

I am compiling one program called nauty. This program uses a canonical function name getline which is also part of the standard GNU C library.

Is it possible to tell GCC at compile time to use this program defined function?

like image 634
Shahzad Avatar asked Dec 11 '22 11:12

Shahzad


2 Answers

One solution:

Now you have declaration of the function in some application .h file something like:

int getline(...); // the custon getline

Change that to:

int application_getline(...); // the custon getline
#define getline application_getline

I think that should do it. It will also fix the .c file where the function is defined, assuming it includes that .h file.

Also, use grep or "find in files" of editor to make sure that every place where that macro takes effect, it will not cause trouble.

Important: in every file, make sure that .h file included after any standard headers which may use getline symbol. You do not want that macro to take effect in those...

Note: this is an ugly hack. Then again, almost everything involving C pre-processor macros can be considered an ugly hack, by some criteria ;). Then again, getting existing incompatible code bases to co-operate and work together is often a case where a hack is acceptable, especially if long term maintenance is not a concern.

Note2: As per this answer and as pointed out in a comment, this is undefined behavior by C standard. Keep this in mind, if intention is to maintain the software for longer then just getting a working executable binary one time. But I added a better solution.

like image 91
hyde Avatar answered Jan 03 '23 20:01

hyde


Note that you may trigger undefined behavior if the GCC header where standard getline is defined is actually used in your code. These are the relevant information sources (emphasis mine):

The libc manual:

1.3.3 Reserved Names

The names of all library types, macros, variables and functions that come from the ISO C standard are reserved unconditionally; your program may not redefine these names. All other library names are reserved if your program explicitly includes the header file that defines or declares them. There are several reasons for these restrictions:

[...]

and the C99 draft standard (N1256):

7.1.3 Reserved identifiers

1

Each header declares or defines all identifiers listed in its associated subclause, and optionally declares or defines identifiers listed in its associated future library directions subclause and identifiers which are always reserved either for any use or for use as file scope identifiers.

[...]

2

No other identifiers are reserved. If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

3

If the program removes (with #undef) any macro definition of an identifier in the first group listed above, the behavior is undefined.

Thus even the macro trick suggested in another post will invoke undefined behavior if you include the header of getline in your code.

Unfortunately, in this case the only safe bet is to manually rename all getline invocations.

like image 29
Lorenzo Donati -- Codidact.com Avatar answered Jan 03 '23 20:01

Lorenzo Donati -- Codidact.com