Thread: Unresolved externals.

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Unresolved externals.

    Hey everyone. I'm having some problems:
    Code:
    (in Socket class definition)
    int Socket::recv(char* buf, int len);
    
    (in separate .cpp)
    int Socket::recv(char* buf, int len)
    {
    	assert(buf != NULL);
    	return ::recv(si->desc, buf, len, 0);
    }
    The above code compiles properly, but if I add the keyword inline to either the prototype or definition, I end up with an unresolved external pertaining to "int __thiscall Socket::recv(char *,int)". However, if I simply 'inline' the function by putting the definition in the class definition and eliminating the prototype, then I have no errors. The only place Socket::recv() is being called, is here:
    Code:
    void readCallback(Socket s, int err)
    {
    	char buf[200];
    	int res = s.recv(buf, 200);
    	if(res == SOCKET_ERROR)
    		MERR("Receive err");
    	else
    		buf[res] = '\0';
    
    	log << buf << std::endl;
    }
    If it makes any difference, readCallback() is being called via a function pointer contained within the Socket object being passed to it.

    Is this a compiler bug (happens both with MSVC 2005 Express Beta 2 and MSVC 6.0 Pro), or is there something I'm doing genuinely wrong?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    I also add the same problems with vc++6
    I think the compiler tries to inline the function in some other file, but the binaries are in a object or different cpp. It can't acess the code to inline it. It's the same behaviour with templates, although I think this behaviour (inline stuff) should't be expected... I'm not the standard expert here.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Inline statements must be in the header files.

    In other words, they must be included by the source files that use them, therefor placing the inlined statement implemenation in the header file will fix that.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>In other words, they must be included by the source files that use them
    The class definition (and therefore the member function prototype) is defined in AESocket.h, which is already included in the source file. If that's not what you meant, can you clarify?

    >>the inlined statement implemenation in the header file
    That's odd. I know this is required for templates, but I don't think I've ever heard of it for inlined functions. Strangely enough, the same thing (prototyped as 'inline' in the header and defined in the .cpp) works OK for a different function, basicSend(), which is also a simple wrapper around an API function.

    Are you sure this is the problem, jverkoey?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Hmm, I could've sworn I read somewhere that inlined functions must be present in the header files....let me check my sources quick.

    -edit-
    Alright, here's where I read it from: Andre Lamothe's Tricks of the 3D Game Programming Gurus, page 1600 in the Summary section, Trick #10 about optimizations:

    Quote Originally Posted by The book
    ...To inline a function, you must move it from the C/C++ source file to a header file, because the compiler literally needs the source of the function, not just the header....
    *shrugs* If that's wrong I'm sorry for quoting a faulty source. However, I've always stuck my inlined functions in header files and haven't had much of a problem....though I honestly don't inline functions too often unless I'm really trying to optimize something.

    -edit 2-
    Also, from the C99 standard:
    An inline function definition does not provide an external definition for the function. Any function call appearing in the source file containing an inline definition will either be satisfied by inlining the function definition at the call site, or by a reference to an externally defined function.
    Last edited by jverkoey; 06-30-2005 at 11:39 PM.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    OK, well, that seems to make sense (where originally the compiler would put a placeholder for the linker to supply the actual location of the described function, for an inlined function the compiler would need to place the actual code of the function). Anyway, putting the definition of the function in the header did get rid of the errors.

    What I don't get, though, is why my original setup works for my basicSend() function but not my recv() function; I did also use basicSend() in main.cpp (and it is NOT defined there, it is defined in AESockIO.cpp), and received no errors. Also, I don't really follow the quote from the standard:
    An inline function definition does not provide an external definition for the function (1). Any function call appearing in the source file containing an inline definition (2) will either be satisfied by inlining the function definition at the call site (3), or by a reference to an externally defined function (4).
    1) What exactly is an 'external definition'? Does it just refer to the 'ordinary' mechanism for function calls, i.e. jump to another location in code and jump back when done?
    2) Does this mean, the source file contains the definition of the inlined function already?
    3) Does this mean, the compiler will inline the code in its output, or does it mean the function definition must be present in the same file as the call site?
    4) Does this part just refer to what happens if the compiler decides to ignore the 'inline' keyword?

    If the answers are {yes, (any), (a), yes}, then unfortunately I don't believe this really indicates (explicitly at least) that the definition *must* be in the header or defined in each .cpp it is used in. Is there more in the standard that pertains to inlined functions, that might be more definitive about this?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Sorry to bump, figured I shouldn't ignore the questions.

    1) What exactly is an 'external definition'? Does it just refer to the 'ordinary' mechanism for function calls, i.e. jump to another location in code and jump back when done?
    I took that to mean an implementation of the function externally (e.g. in a source file), though I could be misinterpreting it. (Oddly....the standard kinda resembles the bible).

    2) Does this mean, the source file contains the definition of the inlined function already?
    I believe it means if you are using an inlined function in a source file somewhere, then it will follow one of those two conditions stated.

    3) Does this mean, the compiler will inline the code in its output, or does it mean the function definition must be present in the same file as the call site?
    It means it will basically copy the code to where the function is called so no jumping is required whatsoever.

    4) Does this part just refer to what happens if the compiler decides to ignore the 'inline' keyword?
    Yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Unresolved Externals
    By nickname_changed in forum C++ Programming
    Replies: 10
    Last Post: 08-31-2003, 06:13 PM
  5. debug to release modes
    By DavidP in forum Game Programming
    Replies: 5
    Last Post: 03-20-2003, 03:01 PM