[cfe-dev] Is 'weak' attribute supported for weak linking?
Anton Smirnov via cfe-dev
cfe-dev at lists.llvm.org
Wed Sep 30 22:45:29 PDT 2015
Hey, guys.
Before creating a bug report i'd like to know if i'm doing this correct or
not.
Originally it's my SO question
<http://stackoverflow.com/questions/32879801/does-llvm-clang-support-weak-attribute-for-weak-linking>
but feel free to repond here or on SO.
I'm learning some Arduino library sources (HardwareSerial.cpp to be more
detailed) and i've found some interesting attribute weak that i've never
used before:
#if defined(HAVE_HWSERIAL0)
void serialEvent() __attribute__((weak));
bool Serial0_available() __attribute__((weak));#endif
I've found it interesting and i've read that linker should set it to NULL
if it's not defined.
However in my tests with clang i'm unable to use it.
lib.cpp:
#include "lib.h"#include <stdio.h>
void my_weak_func() __attribute__((weak));
void lib_func() {
printf("lib_func()\n");
if (my_weak_func)
my_weak_func();}
lib.h:
#ifndef LIB_FUNC#define LIB_FUNC
void lib_func();
#endif
main.cpp:
#include "lib.h"#include <stdio.h>
#ifdef DEFINE_WEAKvoid my_weak_func() {
printf("my_weak_func()\n"); }#endif
int main() {
lib_func();
printf("finished\n");
return 0;}
if i use g++ lib.cpp main.cpp -o main -DDEFINE_WEAK i'm able to use it:
MBA-Anton:Weak_issue asmirnov$ ./main
lib_func()
my_weak_func()
finished
but if i use g++ lib.cpp main.cpp -o main i'm unable to link the app:
Undefined symbols for architecture x86_64:
"my_weak_func()", referenced from:
lib_func() in lib-ceb555.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
To be more detailed about clang:
MBA-Anton:Weak_issue asmirnov$ g++ --versionConfigured with:
--prefix=/Applications/Xcode.app/Contents/Developer/usr
--with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1Apple
LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)Target:
x86_64-apple-darwin14.3.0Thread model: posix
*What should i do? Is weak attribute supported by llvm/clang?*
PS. I've already tried to rewrite lib.cpp in the way Apple describes
<https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html>
and
still the same linker error:
#include "lib.h"#include <stdio.h>
extern void my_weak_func() __attribute__((weak_import));
void lib_func() {
printf("lib_func()\n");
if (my_weak_func != NULL)
my_weak_func();}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151001/b80c49da/attachment.html>
More information about the cfe-dev
mailing list