link error 2005 or 2001

K

k.jayachandran

I have a very curious and unique problem here.
I'm creating a parser using bison and flex. i did all the development
work in a linux environment.
the project includes the source files output from flex and bison, then
several cpp files to create a data structure from the parser.
i used gcc as the compiler for the flex output(as it is a c file)
and g++ as the compiler for the remaining source files.
linked all the object files to create the final executable file.

when i tried to port the same to the visual studio with the same
settings
i got error as follows

CSymbolTable.obj : error LNK2005: "struct symrec * __cdecl
returnSymbolTable(void)" (?returnSymbolTable@@YAPAUsymrec@@XZ) already
defined in CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "public: struct symrec * __thiscall
CSymbolTable::returnSymbolTable(void)"
(?returnSymbolTable@CSymbolTable@@QAEPAUsymrec@@XZ) already defined in
CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "void __cdecl
enter_or_leave_the_struct(int)" (?enter_or_leave_the_struct@@YAXH@Z)
already defined in CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "public: void __thiscall
CSymbolTable::enter_or_leave_the_struct(int)"
(?enter_or_leave_the_struct@CSymbolTable@@QAEXH@Z) already defined in
CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "char * __cdecl
CreateAnUniqueName(char *)" (?CreateAnUniqueName@@YAPADPAD@Z) already
defined in CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "struct symrec * __cdecl putsym(char
*,int,char *,char *)" (?putsym@@YAPAUsymrec@@PADH00@Z) already defined
in CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "public: struct symrec * __thiscall
CSymbolTable::putsym(char *,int,char *,char *)"
(?putsym@CSymbolTable@@QAEPAUsymrec@@PADH00@Z) already defined in
CSymbolTable.obj
CSymbolTable.obj : error LNK2005: "public: __thiscall
CSymbolTable::CSymbolTable(void)" (??0CSymbolTable@@QAE@XZ) already
defined in CSymbolTable.obj

where CSymbolTable.cpp is one of the source files. this happens for
every source file that i use.
i tried to use ifndef directive to avoid multiple inclusion, then i get
lnk2001 error as follows

parser.obj : error LNK2001: unresolved external symbol "public: void
__thiscall CChannels::AddGroupMeas(char *)"
(?AddGroupMeas@CChannels@@QAEXPAD@Z)
parser.obj : error LNK2001: unresolved external symbol "public: void
__thiscall CChannels::AddASubGroup(char *)"
(?AddASubGroup@CChannels@@QAEXPAD@Z)
parser.obj : error LNK2001: unresolved external symbol "public: void
__thiscall CChannels::AddASubGroup(char *)"
(?AddASubGroup@CChannels@@QAEXPAD@Z)
parser.obj : error LNK2001: unresolved external symbol "public: void
__thiscall CChannels::AddGroupChannel(char *)"
(?AddGroupChannel@CChannels@@QAEXPAD@Z)
parser.obj : error LNK2001: unresolved external symbol "public: void
__thiscall CChannels::AddGroupChannel(char *)"
(?AddGroupChannel@CChannels@@QAEXPAD@Z)
parser.obj : error LNK2001: unresolved external symbol "public: void
__thiscall CChannels::AddAChannel(char *)"
(?AddAChannel@CChannels@@QAEXPAD@Z)

it is very frustrating as it is working very well with gcc and g++

any pointers in the right direction will help me a lot.

thanks again

jc
 
B

Ben Bacarisse

I have a very curious and unique problem here.
I'm creating a parser using bison and flex. i did all the development
work in a linux environment.
the project includes the source files output from flex and bison, then
several cpp

Ah. This is comp.lang.c. You want comp.lang.c++.
 
U

Ulrich Eckhardt

i used gcc as the compiler for the flex output(as it is a c file)
and g++ as the compiler for the remaining source files.
linked all the object files to create the final executable file.

when i tried to port the same to the visual studio with the same
settings

I think you should be able to use just C++ if you want to. However, just
one advise about VS: it differentiates between C and C++ on the extension
of the sourcefile, you need special flags to force it into one mode or the
other when the extension doesn't fit.
i got error as follows

CSymbolTable.obj : error LNK2005: "struct symrec * __cdecl
returnSymbolTable(void)" (?returnSymbolTable@@YAPAUsymrec@@XZ) already
defined in CSymbolTable.obj

Okay, the "?returnSymbolTable@@YAPAUsymrec@@XZ" is the symbol occuring
twice, the part before is the decoded symbol - this is an effect of C++
name mangling, so that file at least was compiled using a C++ compiler.
Anyhow, this particular function is defined twice, once in
CSymbolTable.obj and the other definition in, hehe, CSymbolTable.obj. IOW,
you somehow managed to include that objectfile twice. I guess this is a
handling error of your IDE, so please take this to a forum related to that
rather than clc.
where CSymbolTable.cpp is one of the source files. this happens for
every source file that i use.
i tried to use ifndef directive to avoid multiple inclusion

Wait: please structure your headers so that they can be included more than
once in a single translation unit (i.e. using include guards) and also
that they don't define(!!!) any strong symbols (i.e. no variable
definitions apart from static constant ones and no function definitions
apart from inline functions). This should be very basic C and nothing that
you "try to use" in order to fix problems.

One last thing about integrating C code with C++ code: C++
needs 'extern "C"' on a function declaration in order for it to result in
the same symbol as a C compiler would generate. Further questions should
be in a C++ group though, as this is drifting of the topic of this group.

Lastly, in order to solve such problems, you will have to a) create a
minimal example and b) provide some real code and some real info on what
you did with what file. From your description I can only guess what you
did.

Uli
 
B

Ben Voigt

I have a very curious and unique problem here.
I'm creating a parser using bison and flex. i did all the development
work in a linux environment.
the project includes the source files output from flex and bison, then
several cpp files to create a data structure from the parser.
i used gcc as the compiler for the flex output(as it is a c file)
and g++ as the compiler for the remaining source files.
linked all the object files to create the final executable file.

when i tried to port the same to the visual studio with the same
settings
i got error as follows

CSymbolTable.obj : error LNK2005: "struct symrec * __cdecl
returnSymbolTable(void)" (?returnSymbolTable@@YAPAUsymrec@@XZ) already
defined in CSymbolTable.obj

Somehow you are linking the same .obj file twice, perhaps you are creating a
static library, then the main executable uses both the .cpp file and the
static library?

Since it's a linker and not compiler error, it's unlikely that #ifndef can
help you. Some other things could cause this, but if the code works under
gcc, then it looks like purely a windows makefile (project file) issue.
 
K

k.jayachandran

Hey
thanks for the reply. i got the problem sorted. you guys are right. it
was my bad. the problem being i included all the cpp files in the
source files list and also in the linker setting, i added the object
files.
once i removed the object file names from the link options the lnk2005
error disappeared. now i got the unresolved external variable error.

for example
in cgrammar.tab.cpp
i defined three variables
int start_of_enum_list;
int block_struct_flag;
int bEndOfA2ML;

in lex.yy.c
i defined these three variable as extern

extern int start_of_enum_list;
extern int block_struct_flag;
extern int bEndOfA2ML;

now after compiling i get
lnk2001 error
lex.yy.obj : error lnk2001 : unresolved external symbol _bEndOfA2ML
and for all the three variables

hope i can resolve this

thanks again
jc
 
C

CBFalconer

thanks for the reply. i got the problem sorted. you guys are right.
it was my bad. the problem being i included all the cpp files in the
source files list and also in the linker setting, i added the object
files.
once i removed the object file names from the link options the lnk2005
error disappeared. now i got the unresolved external variable error.

Please don't top-post. Your answer belongs after the quoted (and
snipped) material to which you reply. It may also be intermixed.
The snipping removes material not germane to your reply.

The presence of 'cpp' above indicates that you are confusing C++
with C. Your source files should end in .c, or you are on the
wrong newsgroup.
 
S

santosh


Take note that it's important not to top-post. Refer to the links
below.
(Taken from one of CBFalconer's sigs)
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>

Now coming to your problem:
... now i got the unresolved external variable error.

for example
in cgrammar.tab.cpp
i defined three variables
int start_of_enum_list;
int block_struct_flag;
int bEndOfA2ML;

Check if these are external variables. Also don't qualify them with the
static keyword.
in lex.yy.c
i defined these three variable as extern

extern int start_of_enum_list;
extern int block_struct_flag;
extern int bEndOfA2ML;

Check if you've placed the above declarations outside any functions.

Now try to compile again.
Be warned that linking in C++ is significantly different to C. If your
code is C++, then post future follow-ups to comp.lang.c++.
 
K

k.jayachandran

CBFalconer said:
Please don't top-post. Your answer belongs after the quoted (and
snipped) material to which you reply. It may also be intermixed.
The snipping removes material not germane to your reply.

The presence of 'cpp' above indicates that you are confusing C++
with C. Your source files should end in .c, or you are on the
wrong newsgroup.
i'm sorry. i always forget which is the right way top post or bottom
post
 
K

k.jayachandran

santosh said:
Take note that it's important not to top-post. Refer to the links
below.
(Taken from one of CBFalconer's sigs)
Some informative links:
<<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>

Now coming to your problem:


Check if these are external variables. Also don't qualify them with the
static keyword.


Check if you've placed the above declarations outside any functions.

Now try to compile again.
Be warned that linking in C++ is significantly different to C. If your
code is C++, then post future follow-ups to comp.lang.c++.

lex.yy.c is the output of the flex.
cgrammar.tab.cpp is the output of bison.
lex.yy.c is a C code
cgrammar.tab.cpp is a cpp code

so actually i'm trying to link C and C++ code to get the final output.

in linux i compile lex.yy.c using gcc and the remaining c++ code using
g++ and finally link it with no trouble at all. now when i port the
same to the visual studio i got stuck

i posted in this forum as one part of the problem is a C code

thanks
 
K

Kenneth Brody

CBFalconer wrote: [...]
Please don't top-post. Your answer belongs after the quoted (and
snipped) material to which you reply. It may also be intermixed.
The snipping removes material not germane to your reply.
[...]
i'm sorry. i always forget which is the right way top post or bottom
post

Neither. Inline posting is correct. (On short responses such as
this one, inline and bottom posting appear the same.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
C

CBFalconer

CBFalconer wrote:
.... snip ...

i'm sorry. i always forget which is the right way top post or bottom
post

All you have to think about is an order that makes readable sense.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top