LNK2019 Error under Visual Studio 2010

后端 未结 4 1228
傲寒
傲寒 2021-01-22 19:35

I have created a sample c++ project under Visual Studio 2010 with following files.

A.h

#ifndef A_H
#define A_H

#include 

void foo();

#         


        
相关标签:
4条回答
  • 2021-01-22 19:50

    The code works when I created a new project. I am not sure but the reason of the error might be that I added the folder "C:\Program Files\Microsoft SDKs\Windows\v7.1\Include" to the included directories at the project settings and Visual Studio already included the folder "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include"(which is for the 32 bit programs) by default. So there might be a clash because of that. I added the opengl header files only to the Program Files(x86) include folder, removed the include for 64 bit Program Files folder and the opengl code seems to be working now.

    0 讨论(0)
  • 2021-01-22 20:00

    First, you need to include "stdafx.h" in your "a.cpp" file before the line for including "a.h".

    Second, it is better you add "a.h" into your project "Header Files" and add "a.cpp" into your "Source Files".

    Then it will be compiled without error! Good luck.

    BTW, the reason to include "stdafx.h" is that by default the project use pre-compiled headers that is why the compiler looks for "stdafx.h".

    If you want, you can disable the "precompiled Header" then you don't need "stdafx.h" and everything will be fine.

    0 讨论(0)
  • 2021-01-22 20:11

    I think what happened is that A.h was in the Source group of the project rather than the Header group, so it was compiled as if it were a .cpp. Since both A.cpp and A.h will generate an object file A.obj, the last one to compile is the only one that got linked. I believe the last one compiled was A.h, which didn't have an implementation of foo(), thus the linker couldn't find it.

    0 讨论(0)
  • 2021-01-22 20:12

    EDIT: Nevermind the original answer (below), I believe what you're looking for may be here: Visual Studio 2010's strange "warning LNK4042"

    Original answer (not the problem, but maybe sound advice?): Your header should have guards around it, otherwise it will be defined each time it is called, and cause redefinitions.

    #ifndef A_H
    #define A_H
    
    #include <iostream>
    
    void foo();
    
    #endif //A_H
    
    0 讨论(0)
提交回复
热议问题