LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-06-2014, 11:59 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
Visibility of variables not declared in the header file (GNU C).


Hi: I have three files: usbdrvasm.S, usbdrv.c and usbdrv.h. usbdrvasm.S uses the usbInputBuf variable. The only place where this variable is declared is usbdrv.c. Now, usbdrvasm.S includes the usbdrv.h:
Code:
#include "usbdrv.h"
But usbInputBuf is nowhere to be seen in usbdrv.h. How can usbdrvasm.S see it?

EDIT: maybe it is resolved at linking time? But there is no extern declaration in usbdrvasm.S.

Last edited by stf92; 01-07-2014 at 12:23 AM.
 
Old 01-07-2014, 03:09 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Now you do get a compilation error or you don't?
 
Old 01-07-2014, 04:47 AM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
No I don't.
 
Old 01-07-2014, 05:01 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Then 'usbInputBuf' is somehow defined az 'extern' (or similiar) in usbdrvasm.S
Try this: 'nm usbdrvasm.o | grep usbInputBuf'
 
Old 01-07-2014, 05:58 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Code:
semoi@server:~/takefuji_2013/02$ nm usbdrvasm.o | grep usbInputBuf 
         U usbInputBuf
semoi@server:~/takefuji_2013/02$
Magic! I tell you usbInputBuf is not defined as extern. The only place where usbInputBuf is defined is usbdrv.c.

Last edited by stf92; 01-07-2014 at 06:03 AM.
 
Old 01-07-2014, 07:44 AM   #6
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 328

Rep: Reputation: 74
can you post (segment of) sources and compile command?
 
Old 01-07-2014, 08:17 AM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
usbdrv.c:
Code:
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "usbdrv.h"

/*
General Description:
This module implements the C-part of the USB driver. See usbdrv.h for a
documentation of the entire driver.
*/

/* ------------------------------------------------------------------------- */

/* raw USB registers / interface to assembler code: */
/* usbRxBuf MUST be in 1 byte addressable range (because usbInputBuf is only 1 byte) */
static char     usbRxBuf[2][USB_BUFSIZE];/* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */
uchar           usbDeviceId;            /* assigned during enumeration, defaults to 0 */
uchar           usbInputBuf;            /* ptr to raw buffer used for receiving */
uchar           usbAppBuf;                      /* ptr to raw buffer passed to app for processing */
The only places in usbdrvasm.S where the variable is used are:
Code:
       lds             YL, usbInputBuf ;2 -> 8
       ..........................................
       lds             YL, usbInputBuf         ;2 reposition to buffer start
       ..........................................
       sts             usbInputBuf, x1         ;2 buffers now swapped
As to usbdrv.h, it does not mention the variable at all. The make file is:
Code:
semoi@server:~/takefuji_2013/02$ cat Makefile
TARGET = attiny85

COMPILE = avr-gcc -Wall -O2 -Iusbdrv -I. -mmcu=$(TARGET) # -DDEBUG_LEVEL=2

OBJECTS = usbdrv.o usbdrvasm.o da.o


all:	da.hex

.c.o:
	$(COMPILE) -c $< -o $@

.S.o:
	$(COMPILE) -x assembler-with-cpp -c $< -o $@

.c.s:
	$(COMPILE) -S $< -o $@

clean:
	rm -f da.hex da.lst da.obj da.cof da.list da.map da.eep.hex da.bin *.o da.s 

da.bin:	$(OBJECTS)
	$(COMPILE) -o da.bin $(OBJECTS) -Wl,-Map,da.map

da.hex:	da.bin
	rm -f da.hex da.eep.hex
	avr-objcopy -j .text -j .data -O ihex da.bin da.hex

cpp:
	$(COMPILE) -E da.c
semoi@server:~/takefuji_2013/02$
 
Old 01-07-2014, 08:32 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Then it would seem that the writers of that module did not intend to make that variable externally visible ... or, at least, they did not consider it "safe" to let other modules refer to it willy-nilly. They wanted it to be private, insofar as "C" supports such a notion.

There must have been a good reason . . .
 
1 members found this post helpful.
Old 01-07-2014, 09:54 AM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
http://technofetish.net/repos/buffal...rv/usbdrvasm.S:
Code:
/* Some assembler dependent definitions and declarations: */

#ifdef __IAR_SYSTEMS_ASM__

#   define nop2     rjmp    $+2 /* jump to next instruction */
#   define XL       r26
#   define XH       r27
#   define YL       r28
#   define YH       r29
#   define ZL       r30
#   define ZH       r31
#   define lo8(x)   LOW(x)
#   define hi8(x)   ((x)>>8)    /* not HIGH to allow XLINK to make a proper range check */

    extern  usbRxBuf, usbDeviceAddr, usbNewDeviceAddr, usbInputBuf
Quote:
Originally Posted by sundialsvcs
Then it would seem that the writers of that module did not intend to make that variable externally visible
The comments indicate they intended the variable to be used by the assembly code:
Code:
/* raw USB registers / interface to assembler code: */
Since the assembler can't use C declarations there is no use putting it in the .h file...

Last edited by ntubski; 01-07-2014 at 08:47 PM. Reason: ":" got included in url
 
2 members found this post helpful.
Old 01-07-2014, 04:50 PM   #10
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
If the assembler can't use C declarations, how does it do to use usbInputBuf?
 
Old 01-07-2014, 07:56 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by stf92 View Post
If the assembler can't use C declarations, how does it do to use usbInputBuf?
By using assembler declarations (see highlights in post #9).
 
Old 01-07-2014, 08:11 PM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
I see. But that is not my original usbdrvasm.S. In mine there are not such declarations, notwithstanding the fact that every thing compiles well.
 
Old 01-07-2014, 08:57 PM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by stf92 View Post
But that is not my original usbdrvasm.S. In mine there are not such declarations, notwithstanding the fact that every thing compiles well.
Huh. That makes no sense. What's the output of
Code:
avr-gcc -E -I. -Iusbdrv usbdrvasm.S | grep -F usbInputBuf
 
Old 01-07-2014, 09:58 PM   #14
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Code:
semoi@server:~/takefuji_2013/02$ avr-gcc -E -I. -Iusbdrv usbdrvasm.S -mmcu=attiny85| grep -F usbInputBuf
 lds r28, usbInputBuf ;2 -> 8
 lds r28, usbInputBuf ;2 reposition to buffer start
 sts usbInputBuf, r16 ;2 buffers now swapped
semoi@server:~/takefuji_2013/02$
 
Old 01-08-2014, 03:20 PM   #15
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Apparently GNU assembler doesn't need extern, from https://sourceware.org/binutils/docs/as/Extern.html:
Quote:
.extern is accepted in the source program—for compatibility with other assemblers—but it is ignored. as treats all undefined symbols as external.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] file name too long to be stored in a GNU multivolume header Thymox Linux - Software 5 11-11-2017 02:00 PM
problem in accessing variables declared in another perl script john83reuben Programming 3 03-21-2008 02:45 AM
Unable to export variables declared in a script joehansen12 Linux - Newbie 2 10-26-2005 02:25 PM
declare and initialize variables in a header file alaios Programming 3 09-04-2005 04:25 AM
GNU libc installation to setup cross compiler env - kernel header file TOO OLD !! tanch00 Linux - Software 1 02-06-2002 02:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration