LinuxQuestions.org
Review your favorite Linux distribution.
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 03-24-2006, 09:20 AM   #1
bedoni
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Rep: Reputation: 0
fopen fclose in class constructor


hi

I am getting an "[Linker Error] unresolved external 'fopen' referred from ...", when I want to use this function in the constructor of a class. I included stdio.h and there are other occasions of fopen (not in the constructor), which don't make problem. The parameter file_name is static, defined out of the class in a separate namespace. environment: borland C++ builder 5.

Does someone know something about this problem?

thanx bedoni
 
Old 03-24-2006, 11:59 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There should be no problem using "fopen()" in a constructor - or a destructor, or a class method, or anywhere else in any C++ program.

Please try this:
1. Write a simple, standalone C++ "hello world" (with an fopen in a constructor)
2. If it works, figure out what's different between your test program and your app
3. If it doesn't work, then post your code, and cut/paste the exact error message

I suspect the problem is a simple typo somewhere in your code...

'Hope that helps .. PSM
 
Old 03-24-2006, 02:24 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You probably want to include <stdio> rather than <stdio.h> But without seeing the code it's rather difficult to be sure.
 
Old 03-24-2006, 04:08 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

This example "hello world" compiles, links and runs fine under both MS Visual C++ (Windows) and Gnu C++ (Suse Linux):

Code:
/*
 * x: test program calling stdio "fopen()" from a C++ constructor
 *
 * BUILD:
 * - GC++ 3.3.4:
 *   g++ -g -o x x.cpp
 * - MSVC++ 6.0:
 *   vcvars32
 *   cl -GX x.cpp
 *
 * SAMPLE OUTPUT:
 * C:\temp>x
 * Constructor: Successfully wrote file "x.txt"!
 * 
 * C:\temp>type x.txt
 * Hello from x!
 */
#include <stdio.h>
#include <iostream>

#if defined(WIN32)
  #include <windows.h>
  #define ERRNO GetLastError ()
#else
  #include <errno.h>
  #define ERRNO errno
#endif

class MyClass {
public:

  MyClass (const char *progname) 
  {
    FILE *fp = fopen ("x.txt", "w");
    if (fp != NULL)
    {
      fprintf (fp, "Hello from %s!\n", progname);
      fclose (fp);
      std::cout << "Constructor: Successfully wrote file \"x.txt\"!\n";
    }
    else
    {
      std::cout << "Unable to open file x.txt, errno: " << ERRNO << std::endl;
    }
  }

  ~MyClass () 
  {
    std::cout << "Done with MyClass..." << std::endl;
  }

};

int
main (int argc, char *argv[])
{
  MyClass *c = new MyClass (argv[0]);
  delete c;
  return 0;
}

Last edited by paulsm4; 03-24-2006 at 04:11 PM.
 
Old 03-24-2006, 04:29 PM   #5
addy86
Member
 
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332

Rep: Reputation: 31
What compiler-/linker-options did you use?

Last edited by addy86; 03-24-2006 at 04:33 PM.
 
Old 03-25-2006, 10:20 AM   #6
bedoni
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Original Poster
Rep: Reputation: 0
thanks, and more info

hi
thanks for replies, the code is spread in two files, one .h and one .cpp. (btw, in g++ i can compile, build and make (but not run, because of some other bug), so it might be borland-related shit.)

pile_engine.h:
--------------
#include <stdio.h>

namespace confined{
static char *full_path = NULL,
}

class pile_engineublic pile_access{public:

pile_engine( short P_MB,
unsigned char bits_for_QualifieRs,
P_interrupt_TYPE *interrupt,
char *path
); // Class Constructor
}

pile_engine.cpp:
----------------
pile_engine:ile_engine
( short P_MB,
unsigned char bits_for_QualifieRs,
P_interrupt_TYPE *interrupt,
char *path )

{confined::full_path = new char[ path ];
FILE *F = fopen(confined::full_path, "r" );
bool Relational_Base_exists = ( F != NULL );
fclose(F);

if( Relational_Base_exists )
{STOP(); // take from HD
}
else
{ // create new
}
}

of course there is more code, but I pasted the relevant pieces, I think.

As for the compiler-/linker-options:

=>Compiler:
Code optimization: none
Warnings: selected
Debug Information: Y
Line nb. info... : Y
Disable inline expansions: Y
Pre-compiled Headers: none
Stack-frames: Y

=>Advanced Compiler:
Instruction set: Pentium Pro
Data alignment: Double word
Calling convention: C
Register variables: None
Output: Generate underscores
Floating Point: None
Langage compliance: Borland
Source: Identifier length(z): 250

=>C++:
Member pointers: Smallest
Honor member precision: Y
Zero length empty base classes: Y
Zero length empty class members: Y
Virtual tables: Y

=>Linker:
create debug info: Y
use dynamyc TRL: N
use debug libraries: N
Generate import library: Y
Don't generate state files: Y
max erros: 0
map file: off
warning: selected
min heap: 0x1000
max heap: 0x100000
image base: 0x100000
subsystem major: 4
subsystem minor: 0

Thanks, bedoni
 
Old 03-25-2006, 01:10 PM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Please use code tags when posting:
Code:
pile_engine.h:
--------------
#include <stdio.h>

namespace confined
{
  static char *full_path = NULL,
}

class pile_engine : public pile_access
{
public:

  pile_engine (short P_MB, unsigned char bits_for_QualifieRs, P_interrupt_TYPE *interrupt, char *path ); // Class Constructor
}

pile_engine.cpp:
----------------
pile_engine :: pile_engine (short P_MB, unsigned char bits_for_QualifieRs, P_interrupt_TYPE *interrupt, char *path )
{
  confined::full_path = new char[path];
  FILE *F = fopen (confined::full_path, "r" );
  bool Relational_Base_exists = (F != NULL);
  fclose (F);

  if (Relational_Base_exists)
  {
    STOP(); // take from HD
  }
  else
  {
    // create new
  }
}
And please post the linker message you get trying to compile this snippet.

Thanx in advance .. PSM
 
Old 03-25-2006, 08:45 PM   #8
bedoni
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Original Poster
Rep: Reputation: 0
linker error

The message I get is:

[Linker Error] Unresolved external 'fopen' referenced from E:\pile\easy_engine.obj
[Linker Error] Unresolved external 'fclose' referenced from E:\pile\easy_engine.obj

Thanks bedoni
 
Old 03-25-2006, 10:20 PM   #9
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
I'm surprised there isn't more to the message than that (an error number, for example).

But yes, it sounds like something in Borland C++ Builder 5 (quite old, as I'm sure you're aware).

SUGGESTIONS (if you still really want to get to the bottom of it):

1. Try the "hello world" approach I suggested (heck, cut/paste the "hello world" program I gave you). I'm sure you'll find the problem actually has nothing to do with calling "fopen()" in a constructor. My guess is it might instead have to do with your use of "namespace confined" and/or your parent class "pile_access" (which I don't believe you've shown us yet).

2. Set the Borland "generate mapfile" option to "ON".
This will give you vital clues.

3. Export your IDE configuration to a Borland makefile.

Acquaint yourself with bcc32.cfg and ilink32.cfg. They are your friends.

Experiment with the *order* in which you specify your libraries: it's critical.

4. Iterate between steps 1, 2 and 3; adding and removing stuff from your "hello world"
and adding and removing stuff from your .cfg and makefiles until you resolve the problem.

'Hope that helps .. PSM
 
Old 03-26-2006, 10:25 AM   #10
bedoni
LQ Newbie
 
Registered: Feb 2006
Posts: 4

Original Poster
Rep: Reputation: 0
thanks

thank you for your explicit suggestions, I'll try it out - and let you know when I will get enlighted... (propably I'll get grey meanwhile ), thanks already, bedoni
 
  


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
Undefined references to vtable in class constructor? RavenOfOdin Programming 2 03-01-2006 05:46 PM
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
copy constructor for class containing array of vaiable size objects ? qwijibow Programming 4 12-21-2005 09:50 PM
fclose... what is better os2 Programming 1 05-19-2005 08:25 AM
Class constructor not being called ChimpFace9000 Programming 1 06-03-2002 08:54 PM

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

All times are GMT -5. The time now is 01:46 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