5

let's say I link my application with two static libraries. In both libraries we can find functions with same names but different functionality, what leads to symbol clash. My usual approach - edit source code of libraries and add unique prefix to name of each function. But I don't wanna do this manually anymore. Is there some tool or smart way to add custom prefix to all functions in selected source files? I work either on linux and windows with Visual Studio 2010, so I can use such tool on any of these two platforms.

mszabc
  • 525
  • 5
  • 18
  • Ideally you want to have namespaces, but sadly C does not support them :( – Sergey L. Oct 29 '13 at 16:51
  • Take a look at this question: http://stackoverflow.com/questions/6940384/how-to-deal-with-symbol-collisions-between-statically-linked-libraries – Matthias Oct 29 '13 at 16:54
  • Ohh, seem like I wasn't clear enough. I literally want to modify source code and just looking for some tool to do that. Something like: recognize functions in text file and add prefix. This is multiplatform project, so things like objcopy are no option for me. – mszabc Oct 29 '13 at 17:06
  • I suppose you could do this fairly easily with libclang (which has python bindings) but I don't know of any tool which has been written. That doesn't mean there isn't one, since it seems like it would make a good libclang tutorial project. – rici Oct 29 '13 at 19:24
  • maybe this is also useful: https://stackoverflow.com/questions/1489932/how-to-concatenate-twice-with-the-c-preprocessor-and-expand-a-macro-as-in-arg – Bernardo Ramos Mar 10 '18 at 04:37

2 Answers2

1

Generally changing function signatures in libraries is a really bad idea. Expecially if you're working in a pre-existing code base. If somebody else is using that function, you'll break their code.

Even if it's just your own project, you probably shouldn't get in that habit.

Why don't you just fully specify the functions that clash? Instead of just letting the using Library1; using Library2; handle it, you might try explicitly specifying Library1.Function1() in ambiguious cases.

EDIT: Ah, I thought I saw C++, this answer is completely invalid without namespaces. This might help though

Edit2: objcopy seems to skirt the problem of breaking signatures for others by only prefixing a temporary copy. The link Mathias posted has a full explanation, I'll relink here for visibility

Community
  • 1
  • 1
NathanTempelman
  • 1,301
  • 9
  • 34
  • He said he works in linux though, at least partially – NathanTempelman Oct 29 '13 at 17:04
  • I'd be inclined to use a preprocessor trick to rename all the troublesome symbol, something like -Doldname=new_oldname. It's really low-tech, but can be incorporated into most build systems – Anya Shenanigans Oct 29 '13 at 17:08
  • Yeah, but I would need to know all names to do that. this is something I want to avoid. Once I have list of functions I can even do search & replace in IDE. – mszabc Oct 29 '13 at 19:39
  • Alright, why not just write yourself a script that prefixes every function in a bunch of files? If you can't pull it off, I can probably give it a go when I've got some time. Seems like you could do it fairly easily with some regex. – NathanTempelman Oct 29 '13 at 20:45
1

Alright, I couldn't find you a tool so I wrote you one. I wanted to learn Ruby anyway.

#!/usr/local/bin/ruby -w

while file = ARGF.read do
  methodNames = file.scan(/\w+ (\w*) ?\([\w ,]*\);/).flatten;

  methodNames.each { |methodName|
    file.gsub!(methodName, "prefix"+methodName)}
end

You should be able to run that from command line and feed it all your source files as options. It'll take all the method names declared at the top of your file, and go through the source code prefixing "prefix" to them.

You can change prefix in the script if you like, or figure out how to feed it options as well as filenames.

For example, if you save it in a ruby file called "prefixer.rb" you'll be able to call it from command line like this:

prefixer.rb source1.c source2.c 

Or possibly

prefixer.rb *.c

Hope that does it for you, let me know if you use it.

NathanTempelman
  • 1,301
  • 9
  • 34