473,386 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Linking to a global variable in a static library fails

Hi,

I'm using a static library in my application which links fine except
for a few global variables. The static library only contains a bunch
of .cpp and .h files and the global variables are defined as follows:

extern unsigned mgl_numg, mgl_cur;
extern float mgl_fact;
extern long mgl_gen_fnt[516][6];
extern short mgl_buf_fnt[246080];
const float mgl_fgen = 4*14;
extern mglTeXsymb mgl_tex_symb[];

This gives the following linker errors:

1>mathgl.lib(mgl_font.obj) : error LNK2001: unresolved external symbol
"short * mgl_buf_fnt" (?mgl_buf_fnt@@3PAFA)
1>mathgl.lib(mgl_font.obj) : error LNK2001: unresolved external symbol
"float mgl_fact" (?mgl_fact@@3MA)
1>mathgl.lib(mgl_font.obj) : error LNK2001: unresolved external symbol
"struct mglTeXsymb * mgl_tex_symb" (?mgl_tex_symb@@3PAUmglTeXsymb@@A)
1>mathgl.lib(mgl_font.obj) : error LNK2001: unresolved external symbol
"long (* mgl_gen_fnt)[6]" (?mgl_gen_fnt@@3PAY05JA)
1>mathgl.lib(mgl_font.obj) : error LNK2001: unresolved external symbol
"unsigned int mgl_cur" (?mgl_cur@@3IA)
1>mathgl.lib(mgl_font.obj) : error LNK2001: unresolved external symbol
"unsigned int mgl_numg" (?mgl_numg@@3IA)

When I remove the extern keywords the linker errors are gone, except
for the mgl_tex_symb[] variable. Removing the extern keyword are not
an acceptable solution though since these variables are used by
another .cpp file in the library.

Is there something special about linking to global variables in a
static library? Should they be defined in the header which is included
in the main program as well? The library is a third party library so I
do not really want to start changing things in it, hopefully this can
be avoided by doing something in the header.

My environment is Visual Studio 2008 and both the application and
library is compiled with it.

Thanks
Jaco

Jul 25 '08 #1
1 9875
On Jul 25, 10:29 am, Jaco Naude <naude.j...@gmail.comwrote:
I'm using a static library in my application which links fine
except for a few global variables. The static library only
contains a bunch of .cpp and .h files and the global variables
are defined as follows:
extern unsigned mgl_numg, mgl_cur;
extern float mgl_fact;
extern long mgl_gen_fnt[516][6];
extern short mgl_buf_fnt[246080];
const float mgl_fgen = 4*14;
extern mglTeXsymb mgl_tex_symb[];
With the exception of mgl_fgen, those aren't definitions; just
declarations. Where are the variables defined?

[...]
When I remove the extern keywords the linker errors are gone,
except for the mgl_tex_symb[] variable.
Without the 'extern', or with an initializer, these become
definitions. Except for mgl_tex_symb, which becomes an error
(and shouldn't compile).
Removing the extern keyword are not an acceptable solution
though since these variables are used by another .cpp file in
the library.
In other words, the extern declarations should be in a header
file. But you still need a definition somewhere. For "objects"
(i.e. data, but not references, functions or types), a
declaration at namespace scope is a definition if either 1) it
doesn't contain the storage class specifier "extern", or 2) it
has an explicit initializer. (And I know, there's nothing
really logical about this. It's just the way it is.)
Is there something special about linking to global variables
in a static library?
No.
Should they be defined in the header which is included in the
main program as well?
Not defined, but if the main program uses them, they should be
declared. As I said, the "standard" solution is to declare them
in a header, and include that. If the declarations differ in
the slighest detail, all sorts of problems may occur, and the
simplest way of ensuring that they don't differ is to only have
one instance of them, in a header.
The library is a third party library so I do not really want
to start changing things in it, hopefully this can be avoided
by doing something in the header.
Are you sure you've compiled and included all of the source
files in the library. If there's only a declaration, and no
definition, this shouldn't compile and link anywhere (but it is
formally undefined behavior, so it's just possible that some
compiler accept it).

If I understand the C standard correctly, it's rules ultimately
lead to the same results (although they are stated in a
radically different manner).
My environment is Visual Studio 2008 and both the application
and library is compiled with it.
It shouldn't make a difference, although as I daid, it's
undefined behavior, so the standard doesn't really require an
error.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 25 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: j | last post by:
Anyone here feel that "global variables" is misleading for variables whose scope is file scope? "global" seems to imply global visibility, while this isn't true for variables whose scope is file...
7
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with...
15
by: randyr | last post by:
I am developing an asp.net app based on a previous asp application. in the asp applications global.asa file I had several <object id="id" runat="server" scope="scope" class="comclass"> tags for...
3
by: Charles Nicholson | last post by:
Hello all- I have some static C++ libraries that I wrote in VS2003 but which upgraded fine when i went to VS2005 Pro. In them i overload the global versions of operators new, new, delete, and...
44
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
37
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in...
0
by: Roux Claude | last post by:
Bonjour, We have implemented Python as an "embedded script language" in our NLP application, XIP which a grammar rule engine written in C++ and available as a library. We have also developped...
3
by: Pixel.to.life | last post by:
Hi, Gurus, I recently attempted to build a .Net forms application, that links with old style unmanaged C++ static libs. Of course I had to recompile the static lib projects to link properly...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.