Thread: File Linking Problem

  1. #16
    Yin
    Guest

    Unhappy

    Originally posted by M.shawan
    class abc
    {
    private:
    int x; // you have not initial x direct with 99.

    public:
    abc() { x=99;} // constrctur initial the member x with 99.

    int get_x() { return x; }
    };

    //The content of second.cc is:

    #include <iostream.h> // syntax error"iostresm"
    extern class abc test_abc;

    int main() {

    abc test_abc; // create an test_abc object.

    cout << test_abc.get_x() << endl;

    return (0);

    }

    Thanks. But if you have read my second message, you would know that I have used iostream already.

    Now, your suggestion creates an error when compiling second.cc:
    " In function 'int main()' :
    aggregate 'class abc test_abc' has incomplete type and cannot
    be initialized. "

    So, any other suggestions? =(

    P.S. extern.... It drive me crazy, how to use it with class object?

  2. #17
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    You are going to need zen, stoned_coder, or nvoigt's help with this. They are very familiar with 'extern'. I have used it to incorporate assembly functions into my C++ programs, but that is about the extent of it.

    C++ has a different naming convention than C. The compiler names your functions something funky instead of _function(). In order to call a C naming convention, you need to declare your function a C style naming convention and then

    extern "C"
    {
    void function(); // or in this case class
    }

    then function would be named _function() by the compiler and your linker would look for an object file that contains _function().

    How that incorporates with classes, I am unsure. And how you identify your class name as a C naming convention, I am also unsure.

    That is of course if you even need to go through all that pain.

    PM one of the experts mentioned above. When you find a solution, let us know so that we have a better understanding of extern.

    Good Luck...

    Betazep
    Blue

  3. #18
    Yin
    Guest
    Thanks betazep.

    Before I PM one of those guy you mentioned, I would like to restate again my situation in case some of you get fussy while reading all these long post.

    Say, I have two file:

    within file1.cc
    int i = 0;

    within file2.cc
    extern int i;
    int main(){i; return 0;}

    There is NO problem in this case, however when the variable i is changed to a class object, problem ARISED:

    I have two file, one for defining the class, namely first.cc :

    class abc {
    private:
    int x;
    public:
    abc(void) { x=99; }
    int get_x() { return x; }
    };

    The second file contains the main program which utilize the above class, namely second.cc :

    #include <iostream.h>
    extern class abc test_abc;

    int main() {
    cout << test_abc.get_x() << endl;
    return (0);
    }

    Compiling the first file into object file (by "g++ -c -o first.o first.cc"
    in cygwin ) has absolutely NO problem.

    But compiling the second file always lead to ERROR message. But I think I
    have used the syntax "extern" appropiately. The compiler know that test_abc is a foreign object. However it doesn't know get_x() is a foreign function. It always reply me as "no function for call to abc:: get_x()". Isnt it ridicuious that the compiler knows the object is foreign, but it dunno the member function of the object is foreign, too?

    Thats my question. And I must not use any header file, too.

    I know that a header file is appropiate, but I don't want to use it.
    Because a header file is not possible to be encrypted. I mean that I want other to use my class, but I don't want them to know my content. If I am able to compile it into an object file, which is human unreadable, the computer is still able to read it. You may wonder then how they know how to use my class then. Actually I will tell them the use of the my class, but not the actual algorithm within the class.

    I hope I have made it clear.
    Thanks for reading. Any advice? ^_^

  4. #19
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    extern int i

    would work because it is an int... try the same thing with a function.
    Blue

  5. #20
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I have tried many different ways to implement that. I am at a loss...
    Blue

  6. #21
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    well there is one way... but you probably don't want to do this either.


    Code:
    file test.cpp+++++++++++++++++++
    
    class test
    {
    
       public:
                   test() { x=99; }
                   int ret_x() { return x; }
    
       private:
                   int x;
    
    };
    
    ++++++++++++++++++++++++++++
    
    file test2.cpp+++++++++++++++++++
    
    #include <iostream>
    #include "test.cpp"
    using std::cout;
    
    int main()
    {
       test testing;
    
       cout << testing.ret_x();
    
    return 0;
    } // end main
    
    +++++++++++++++++++++++++++++
    Then just compile test2.cpp and your class is linked in. You are going to need to get help from one of the more knowledgeable people here on extern. I have tried countless ways to do it....
    Blue

  7. #22
    Unregistered
    Guest
    Hey, Yim!
    "int" is a standard, build-in, type of compiler. But "abs" is your user-defined type. So you must tell your compiler what is a type "abs" in second file. The keyword "extern" concerns to a variable, not to a type, which must be defined as well.

  8. #23
    Yin
    Guest
    Originally posted by Betazep
    I have tried many different ways to implement that. I am at a loss...
    Thanks for the trials... But your way still need the visible file test.cpp to be present when compiling... So that's no a perfect way yet.

  9. #24
    Yin
    Guest
    Originally posted by Unregistered
    Hey, Yim!
    "int" is a standard, build-in, type of compiler. But "abs" is your user-defined type. So you must tell your compiler what is a type "abs" in second file. The keyword "extern" concerns to a variable, not to a type, which must be defined as well.
    I understand that abc is my user-defined type. Therefore instead of writing "extern abc test_abc;", I wrote "extern class abc test_abc". Therefore one would expect abc to be a class and test_abc is a class object.

    extern concerns to variable, as well as objects. So the compiler knows test_abc is an extern object. But what surprise me is that the compiler don't know that test_abc.get_x() is an extern too.

  10. #25
    Unregistered
    Guest
    Oh!
    Try write "test_abc.get_anything()"! The same error will be encountered. The compiler do not know what methods are in class "abs".
    It is possible in COM technology, not in pure C.

  11. #26
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>So the compiler knows test_abc is an extern object.


    I disagree...

    extern class blah type;

    comes back with an error that pretty much states that class cannot be used as an extern in this fashion.

    extern class blah;

    blah type;

    comes back with a similar error.


    extern blah;

    blah type

    is a completely different error stating something to the fact that blah isn't reconized as an object of type class, struct, whatever.

    Now you really have me wanting to find an answer to this too...

    Perhaps using malloc to set memory aside for a class... hmmm. I need to not be this intrigued while I have finals... you are a bad bad man.
    Blue

  12. #27
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The extern storage modifier tells the compiler to accept the use of the name in the file in which it occurs, but not to allocate storage for the "extern" item, in this case your class.

    Since your file with your main has test_abc declared as external, it allocates no storage for the object.

    Your other file defines the class and it's components, but does not actually create an instance.

    The question you need to ask yourself is, "where is the object?"...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  13. #28
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    The question for me is... "What is the answer?"
    Blue

  14. #29
    Yin
    Guest

    Unhappy

    Originally posted by Unregistered
    Oh!
    Try write "test_abc.get_anything()"! The same error will be encountered. The compiler do not know what methods are in class "abs".
    It is possible in COM technology, not in pure C.
    Yes, the same error will occur. Isn't it ridiculous? the compiler *should* know the method in 'class abc'. Because I have type the line "extern class abc test_abc;" . THe compiler should know that the method in abc is stored in another file and don't bother when compiling. However it echoes me with an error message.

    Betazep, nice to hear that you get involved, hoho. I have a new light to this problem: "pimpl idiom".

    I don't know whether "pimpl idiom" can help or not. But it seems to be something about hiding class content. My knowledge to "pimpl idiom" is very limited however.

    By the way, can did you(Betazep) PM-ed the guys you mentioned for me? I am new to this board and maybe you can help me by sending them PM. Because I don't know them and I didnt do any PM before. Furthermore, I am not registered. :P

    Good Luck, Let's work towards the answer to "class encryption"!

  15. #30
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    I sent a PM to Stoned_Coder...

    he is brilliant. Hopefully he will know.

    I am starting to think that it cannot be done with extern.

    Oh... and register.
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Long file linking problem
    By hypertension in forum C Programming
    Replies: 3
    Last Post: 10-15-2002, 09:55 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM