ChrisZZ
ChrisZZ

Reputation: 2181

how to check and avoid funtions having same names in one static library?

I'm confusing when creating a static library in C/C++ with same funtion names and param lists but implemented in different source files.

Say, I have play() funtion declared in test.h, and play() implemented in both test.c and test_old.c. When creating a library that include both test.c and test_old.c in usually manner, there will be no error.

But this will make people confusing when using this library. How to detect this replicated implementation?Thanks.

Upvotes: 2

Views: 200

Answers (1)

Richard
Richard

Reputation: 1177

Duplicating function names in your static library is very bad practice. Don't do this.

That said you can check for duplicate definitions by examining the output of the nm application.

$ nm libstest.a

test1.c.o:
0000000000000000 T bla

test2.c.o:
0000000000000000 T bla

The following command lists duplicate functions in your library:

$ nm libstest.a | grep -P "^[^\\s]+ T " | cut -d' ' -f3 | sort | uniq -d

Upvotes: 5

Related Questions