Share

ngspice

Tracker: Bugs

5 Undefined reference to history_file - ID: 2874418
Last Update: Settings changed ( h_vogt )

Problem compiling ngspice19 with readline or editline support.

I found an old reference to this in 2007 that was closed.
http://sourceforge.net/tracker/index.php?func=detail&aid=1635787&group_id=3
8962&atid=423915

The variable history_file is defined in main.c and referenced as extern in
misccoms.c but cannot be found by the linker:

frontend/.libs/libfte.a(misccoms.o): In function `byemesg':
/home/ksarkies/Compile/ngspice-19/src/frontend/misccoms.c:250: undefined
reference to `history_file'

A grep on the source files shows

src/main.c:85:char history_file[512];
src/main.c:506: strcpy(history_file, getenv("HOME"));
src/main.c:507: strcat(history_file, "/.");
src/main.c:508: strcat(history_file, application_name);
src/main.c:509: strcat(history_file, "_history");
src/main.c:512: read_history(history_file);

src/frontend/misccoms.c:245: extern char history_file[];
src/frontend/misccoms.c:250: write_history(history_file);

I guess history_file needs to be defined in a header somewhere and made
accessible to both source files, but my C skills don't stretch to fixing
this. I survived by deleting the code at the end of misccoms.c

ngspice is a great piece of work. Thanks to all who contributed.

Ken Sarkies


Jiggerjuice ( ksarkies ) - 2009-10-08 00:55

5

Closed

Fixed

Holger Vogt

frontend

None

Public


Comments ( 6 )

Date: 2009-10-11 16:58
Sender: h_vogtProject Admin

patch uploaded to CVS

Holger Vogt


Date: 2009-10-11 08:31
Sender: nobody

Hi Holger

I'm using gcc 4.2.4 as distributed with Ubuntu 8.04 (a little old now).
Your patch works fine. I agree it is probably going to work better with
different compilers as it removes the guesswork that the compilers have to
do.

For interest I tried compiling the original package on a Fedora machine
(gcc 4.3.2) and it went through without a hitch. The calls to gcc in Ubuntu
seemed to have additional macros compared to Fedora's calls. I guess this
is where the difference lies.

cheers, Ken



Date: 2009-10-10 14:09
Sender: h_vogtProject Admin

Ken,

according to the wiki, initialization of history_file would force the
definition (instead of a declaration).

Could you test please to replace line 81 in main.c
char history_file[512];
by
char history_file[512] = {'\0'};
and remove the extern declaration in function app_rl_readlines()?

If this works for you, we should add it as a patch.

Regards

Holger




Date: 2009-10-10 12:53
Sender: h_vogtProject Admin

Ken,

what might have happended, is found at
http://en.wikipedia.org/wiki/External_variable:

"The extern keyword means "declare without defining". In other words, it
is a way to explicitly declare a variable, or to force a declaration
without a definition.

It is also possible to explicitly define a variable, i.e. to force a
definition. It is done by assigning an initialization value to a variable.

If neither the extern keyword nor an initialization value are present, the
statement can be either a declaration or a definition. It is up to the
compiler to analyse the modules of the program and decide.

A variable must be defined once in one of the modules of the program. If
there is no definition or more than one, an error is produced, possibly in
the linking stage."

The third sentence describes what might have happened. Maybe your
compiler has decided differently to the others we are currently testing.
The addition of the extern statement might have changed the context for the
decision.

What is th OS, the compiler you have been using?

Regards

Holger





Date: 2009-10-10 10:52
Sender: h_vogtProject Admin

Ken,

many thanks for your patch.

In fact K&S state that a global variable has to be declared in each
function where it is used, by an external ... declaration.

The declaration should also be possible by implicit rules from within the
context. Obviously modern compilers have changed these rules a little bit.
If you know more about these changes, please let me know.

I will test and upload your patch.

Regards

Holger


Date: 2009-10-10 02:50
Sender: ksarkies

Discovered this appears in earlier versions as well. I fixed it properly
(and got a side benefit in seeing line editing actually working) by adding

extern char history_file[];

in main.c in the function app_rl_readlines(). I was using gcc 4.2.4 which
may be a bit stricter about C syntax than earlier compilers.

Ken


Attached File

No Files Currently Attached

Changes ( 5 )

Field Old Value Date By
status_id Open 2009-10-11 16:58 h_vogt
allow_comments 1 2009-10-11 16:58 h_vogt
close_date - 2009-10-11 16:58 h_vogt
resolution_id None 2009-10-10 10:52 h_vogt
assigned_to nobody 2009-10-10 10:52 h_vogt