This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
Re: [ros-dev] __attribute__((weak)) - in mingw
- From: Boaz Harrosh <boaz at moeg dot org>
- To: Danny Smith <dannysmith at users dot sourceforge dot net>, ReactOS Development List <ros-dev at reactos dot com>, aaron98wiridge9 at aaronwl dot com, binutils at sources dot redhat dot com
- Date: Tue, 15 Feb 2005 11:54:11 +0200
- Subject: Re: [ros-dev] __attribute__((weak)) - in mingw
- References: <420FDB53.2080406@hishome.net> <000d01c5122a$1051f460$2b4861cb@DANNY>
Danny Smith wrote:
Boaz Harrosh wrote:
[Q] I'm (well ATL is) using __attribute__((weak)) (translated from
__declspec( selectany) ) for instantiation of members and variables in
headers. I had no problem with it On GCC in Linux (gcc 3.2.2). On MinGW
IMAGE_COMDAT_SELECT_ANY is not quite same as PECOFF version of "weak" , but
AFAICT is equivalent to the GCC section flag ".linkonce discard". I don't think
there is a way for the user to specify that for data using an attribute, but it
could be done with asm statements.
.weak directive is partially supported in current binutils CVS.
__attribute__((weak)) is not supported by GCC-3.4.x but will be in the next
major GCC release (4.0.0).
The semantics of weak for PECOFF differ from that on Linux.See the PECOFF60
specs (Microsoft Portable Executable and Common Object File Format
Specification) section on weak externals
Danny
(binutils at sources dot redhat dot com please also cc me as I'm not on
the list)
Attached is a proof (See fooInt.h) that gcc (gcc version 3.2 (mingw
special 20020817-1)) has support for weak symbols. Just not with the
regular syntax.
But when templates are used duplicate symbols are merged by the linker.
What would be the assembler magic to cram into the __WEAK__ definition
that would make this project link?
Free Life
Boaz
# Project: Weak
# Makefile created by Dev-C++ 4.9.8.0
CPP = g++.exe
CC = gcc.exe
WINDRES = windres.exe --include-dir=D:\Dinosaur\OneSource\NeoWine\msvc\atl\include --include-dir=D:\Dinosaur\OneSource\NeoWine\wine\include
RES =
OBJ = main.o cpp2.o $(RES)
LINKOBJ = main.o cpp2.o $(RES)
LIBS = -L"D:/Dinosaur/PTools/Dev-Cpp/lib"
INCS = -I"D:/Dinosaur/PTools/Dev-Cpp/include"
CXXINCS = -I"D:/Dinosaur/PTools/Dev-Cpp/include/c++" -I"D:/Dinosaur/PTools/Dev-Cpp/include/c++/mingw32" -I"D:/Dinosaur/PTools/Dev-Cpp/include/c++/backward" -I"D:/Dinosaur/OneSource/NeoWine/msvc/include" -I"D:/Dinosaur/OneSource/NeoWine/msvc/atl/include" -I"D:/Dinosaur/OneSource/NeoWine/msvc/mfc/include" -I"D:/Dinosaur/OneSource/NeoWine/wine/include" -I"D:/Dinosaur/OneSource/NeoWine/wine/include/msvcrt"
BIN = Weak.exe
CXXFLAGS = $(CXXINCS)
CFLAGS = $(INCS)
.PHONY: all all-before all-after clean clean-custom
all: all-before Weak.exe all-after
clean: clean-custom
rm -f $(OBJ) $(BIN)
$(BIN): $(LINKOBJ)
$(CPP) $(LINKOBJ) -o "Weak.exe" $(LIBS)
main.o: main.cpp
$(CPP) -c main.cpp -o main.o $(CXXFLAGS)
cpp2.o: cpp2.cpp
$(CPP) -c cpp2.cpp -o cpp2.o $(CXXFLAGS)
#include "fooInt.h"
int foo_3()
{
return fooInt*fooInt*fooInt * Static_T<int>::My ;
}
// the proper way would be
#define __WEAK__ __attribute__((weak))
// I have tried extern ,dllimport, but they are all ignored (and warned) because of the
// "= 6". If not for the assignment, extern with changing definition of __WEAK__
// could work. All modules compiled with "extern" and one with out.
// what could be the defenition of __WEAK__ that will make this project to link?
// I thought that a combination of section and asm() could do it but I cannot
// figure out what the magig should be. Weak symbles are supported in templates.
// So what is the compiler's magic for weak templates symbles that make the linker
// happy
__WEAK__ int fooInt = 6 ;
extern int foo_2() ;
extern int foo_3() ;
template <typename T>
class Static_T
{
public:
static T My ;
} ;
template <typename T>
T Static_T<T>::My = 17 ;
#include <stdio.h>
#include "fooInt.h"
int foo_2()
{
return fooInt*fooInt*Static_T<int>::My ;
}
int main(int argc, char *argv[])
{
printf("%d,%d\n" ,foo_2() ,foo_3() ) ;
return 0;
}