在标头中定义符号,其重复实例将由链接器

时间:2017-02-04 00:38:05

标签: c linker

在C中是否有任何方法可以定义符号,以便链接器合并所有缩进的定义?在链接器级别似乎是可能的,因为,例如,C ++支持模板函数和模板静态变量,这些变量完全符合 1

具体来说,我想在头文件int array[]中定义int_array.h这样的int array[42] = {};

int_array.h

当许多单独的编译单元都包含.o时,它们对应的array文件显然会包含array的定义和空间 - 但是当它们链接在一起时,我只想要那个一个#Importing Stuff import pygame import sys import time import random from pygame.locals import* pygame.init() #Naming Variables menu = 0 color = (65,105,225) tcolor = (255,255,255) pcolor = (255,255,255) hcolor = (255,255,255) width, height = 1920, 1080 screen = pygame.display.set_mode((width, height)) hecolor = (255,255,255) sys_font = pygame.font.SysFont \ ("None", 60) #Initializing Screen pygame.display.set_caption("TSA Trend Game") screen.fill(((color))) pygame.display.update() #Making Menu while 1 == 1 and menu == 0: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() #More Variables rendered = sys_font.render \ ("Welcome to Trends of 2016!", True, ((tcolor))) play = sys_font.render \ ("Play Game", True, ((pcolor))) help = sys_font.render \ ("Help", True, ((hcolor))) play_r = play.get_rect() play_r.x, play_r.y = 710, 500 help_r = help.get_rect() help_r.x, help_r.y = 1170, 500 render_r = play.get_rect() render_r.x, render_r.y = 710, 500 #Display Text screen.blit(rendered, (710, 440)) screen.blit(help, (1170, 500)) screen.blit(play, (710, 500)) pygame.display.update() if render_r.collidepoint(pygame.mouse.get_pos()): pcolor = (255,255,0) else: pcolor = (255,255,255) if help_r.collidepoint(pygame.mouse.get_pos()): hcolor = (255,255,0) else: hcolor = (255,255,255) if event.type == pygame.MOUSEBUTTONDOWN and help_r.collidepoint(pygame.mouse.get_pos()): screen.fill(pygame.Color("black")) pygame.display.update() 符号存活,每个人都指向它。

答案不一定是标准C (实际上,链接器并没有真正明确地通过标准解决) - 但它应该通常适用于现代编译器。

1 这是一个C问题,而不是C ++问题,但事实证明每个现代工具链都使用相同的链接器。

1 个答案:

答案 0 :(得分:2)

只是声明他们是弱者:

__attribute__((weak)) int array[42] = {};

有关详细信息,请参阅例如here