My C++ class is having trouble linking to a functi

2019-02-20 15:44发布

I asked the same question yesterday and the answer was not applicable.

https://stackoverflow.com/questions/6194578/breaking-up-class-functions-into-multiple-cpp-files

If I go into the class header, right click on the function and click "Go to Definition" and it takes me right to my function in my other .CPP file. It sees it, can link to it and still I get errors that indicate I cannot see it.

Anyone have any suggestions? I'll try anything.

Here is the error again.

zdll.lib(d000050.o) : warning LNK4078: multiple '.text' sections found with different attributes (E0300020)

WLD.obj : error LNK2019: unresolved external symbol "public: void __thiscall WLD::fragment_03(unsigned char *,int)" (?fragment_03@WLD@@QAEXPAEH@Z) referenced in function "public: bool __thiscall WLD::init(unsigned char *)" (?init@WLD@@QAE_NPAE@Z)

Edit: Also, I am using MSVC++. Should I try creating a new solution and importing the files? May help as I feel I am out of options...

Edit: Here is the code:

#include "WLD.h"

inline void WLD::fragment_03(uchar* location, int frag_num)
{
    // Read the struct into memory and create a temporary pointer
    struct_frag03 temp03;
    memcpy(&temp03, location, sizeof(struct_frag03));
    uchar* temp_p = location;

    // Advance the pointer to the encoded bytes (filename)
    temp_p += sizeof(long) + sizeof(short);

    // Grab the encoded filename and decode it
    uchar* f_filename = new uchar [sizeof(temp03.nameLen + 1)];
    memcpy(f_filename, temp_p, temp03.nameLen + 1);
    decode(f_filename, temp03.nameLen);

    // Add the details about this bitmap to the array
    bmp_array[current_bmp].filename = f_filename;
    bmp_array[current_bmp].nameLength = temp03.nameLen;
    bmp_array[current_bmp].reference03 = frag_num;

    // 0x03 Debug
    //errorLog.OutputSuccess("0x03 Filename: %s", bmp_array[current_bmp].filename);
    //errorLog.OutputSuccess("0x03 Name length: %i",bmp_array[current_bmp].nameLength);
    //errorLog.OutputSuccess("0x03 Reference: %i", bmp_array[current_bmp].reference03);

    // Add the bitmap to the count
    current_bmp++;
}

And here is where the code is called in the WLD class:

case 0x03:
    fragment_03(wld + file_pos + sizeof(struct_wld_basic_frag), i);
    break;

Here is the header file declaration: in (WLD.h):

public:
    inline void fragment_03(uchar* location, int frag_num);

标签: c++ class linker
2条回答
太酷不给撩
2楼-- · 2019-02-20 16:07

inline means that the function effectively has internal linkage (that is, it must exist inside the translation unit where it is used). Either move the definition of the function into the header, or remove inline.

(inline for modern compilers really means "use internal linkage" -- compilers will inline where it makes sense to by themselves, and they typically make better decisions than humans)

EDIT: Technically speaking, the language the standard uses here says that inline functions have external linkage; however, it also says An inline function shall be defined in every translation unit in which it is used. in section 3.2 paragraph #3 of the standard, and also There can be more than one definition of a ... inline function with external linkage (7.1.2) ... Given such an entity named D defined in more than one translation unit ... each definition of D shall consist of the same sequence of tokens in paragraph 5. So while technically speaking the name you declared inline is accessible from outside a given translation unit, to do so is to cause undefined behavior from C++.

查看更多
可以哭但决不认输i
3楼-- · 2019-02-20 16:11

This has nothing to do with includes. What you have there is a linker error. Includes are all about compilation, which requires the declarations of identifiers referred to.
The error you have means that the linker can't find the definition of a function in any of the object files passed to it which is called by one or more of them. (See this answer for what's a declaration and what's a definition and what they are needed for.)

You need to pass to the linker the object files created from compiling all of your .cpp files. If you're using some sort of an IDE, it should do this for you if you add all .cpp files to your project. If you're using a makefile, list all .cpp files as dependencies of your target. If you compile by invoking the compiler manually (which then calls the linker), pass all .cpp files to it in the same invocation.

查看更多
登录 后发表回答