链接器警告每隔几次编译就会导致随机链接错误

时间:2021-03-11 06:50:49

标签: c++ opengl linker glfw glew

我无法获得比我拥有的更小的最小可复制示例。主要是因为我无法调试问题,它似乎完全是随机的,而且我不会一直收到错误消息,有时会编译,有时不会。我已经恢复到出现错误时的状态,以便我可以复制代码,但从那时起,我就没有收到错误消息。基本上,除了修复所有链接器警告之外,我没有其他想法。我记得它主要与此警告有关 LNK4042 object specified more than once; extras ignored in main.obj。还有另一个警告我不明白,因为我有一个名为 GLEW 的库的二进制文件,但静态链接它会调用链接器警告,LNK4099 PDB 'vc120.pdb' was not found with 'glew32s.lib(glew.obj)' or at '\SolutionDir\Debug\vc120.pdb'; linking object as if no debug info

这些警告的修复方法是什么?希望能阻止我随机收到错误,当我更改一件事时,它会编译,然后将其还原,它仍然可以编译。

这是代码,ma​​in.h

#pragma once

#ifndef MAIN_H
#define MAIN_H


#include <GL/glew.h>  // Initialize with glewInit()

#ifndef GLFW_INCLUDE_NONE
#define GLFW_INCLUDE_NONE // GLFW including OpenGL headers causes ambiguity or multiple definition errors.
#endif // GLFW_INCLUDE_NONE

       
#include <GLFW/glfw3.h>


#include "ImGui/imgui.h"
#include "ImGui/implot.h"
#include "ImGui/imgui_impl_glfw.h"
#include "ImGui/imgui_impl_opengl3.h"


#include <iostream> //std::string

 

class Main    ///Singleton
{
public:
    Main(const Main&) = delete;
    Main(Main&&) = delete;
    Main& operator=(const Main&) = delete;
    Main& operator=(Main&&) = delete;



private:
    Main();


    static Main& Get_Instance();



    friend int main(int argc, char* argv[]);


    static void Mainloop();
    static void Init();
    static void Free();



    GLFWwindow* m_Window = nullptr;


    std::string m_GLSL_Version = "";

    int m_Window_Width = 1280;
    int m_Window_Height = 720;


};




#endif // MAIN_H

ma​​in.cpp

#include "main.h"



Main::Main()
{

}



Main& Main::Get_Instance()
{
    static Main instance;
    return instance;
}




void Main::Init()
{

    // Setup window
    Get_Instance(); //Init constructor


    if (!glfwInit()) {
        std::cout << "Could not initialize GLFW" << std::endl;
        std::cin.get();
        exit(EXIT_FAILURE);
    }


    Get_Instance().m_GLSL_Version = "#version 460";
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);


    // Create window with graphics context
    Get_Instance().m_Window = glfwCreateWindow(Get_Instance().m_Window_Width, Get_Instance().m_Window_Height, "Program", NULL, NULL);
    if (Get_Instance().m_Window == NULL) {
        std::cout << "Could not create GLFW window" << std::endl;
        std::cin.get();
        exit(EXIT_FAILURE);
    }


    glfwMakeContextCurrent(Get_Instance().m_Window);


    glfwSwapInterval(0); //  vsync
     


    if (glewInit() != GLEW_OK)
    {
        fprintf(stderr, "Failed to initialize OpenGL loader!\n");
        std::cin.get();
        exit(EXIT_FAILURE);
    }

    // Setup Dear ImGui context
    IMGUI_CHECKVERSION();
    ImGui::CreateContext();
    ImPlot::CreateContext();

    // Setup Dear ImGui style 
    ImGui::StyleColorsClassic();

    // Setup Platform/Renderer backends
    ImGui_ImplGlfw_InitForOpenGL(Get_Instance().m_Window, true);
    ImGui_ImplOpenGL3_Init(Get_Instance().m_GLSL_Version.c_str());

}




void Main::Mainloop()
{


    // Main loop
    while (!glfwWindowShouldClose(Get_Instance().m_Window))
    {


        //Events
        glfwPollEvents();


        // Start the Dear ImGui frame
        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplGlfw_NewFrame();
        ImGui::NewFrame();



        ImGui::Text("%.3f", ImGui::GetIO().Framerate);



        // Rendering
        ImGui::Render();
        glfwGetFramebufferSize(Get_Instance().m_Window, &Get_Instance().m_Window_Width, &Get_Instance().m_Window_Height);
        glViewport(0, 0, Get_Instance().m_Window_Width, Get_Instance().m_Window_Height);
        glClearColor(0.45f, 0.55f, 0.60f, 1.00f);
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());

        glfwSwapBuffers(Get_Instance().m_Window);
    }

  

}





void Main::Free()
{

    // Cleanup
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
    ImPlot::DestroyContext();
    ImGui::DestroyContext();


    glfwDestroyWindow(Get_Instance().m_Window);
    glfwTerminate();
}
  





int main(int argc, char* argv[])
{

    Main::Init();
    Main::Mainloop();
    Main::Free();


    return 0;
}



编辑:它又发生了,这里是错误:

Error   LNK1561 entry point must be defined
Warning LNK4042 object specified more than once; extras ignored (THIS IS IN MY main.obj file, its the only linker issue that references my actual files, the rest reference either 'LINK' or 'libucrt.lib(something here)'
Error   LNK2005 __cexit already defined in ucrtd.lib(ucrtbased.dll) 
Error   LNK2005 __crt_atexit already defined in ucrtd.lib(ucrtbased.dll)    
Error   LNK2005 __crt_at_quick_exit already defined in ucrtd.lib(ucrtbased.dll) 
Error   LNK2005 __execute_onexit_table already defined in ucrtd.lib(ucrtbased.dll)
Error   LNK2005 __initialize_narrow_environment already defined in ucrtd.lib(ucrtbased.dll) 
Error   LNK2005 __initialize_onexit_table already defined in ucrtd.lib(ucrtbased.dll)
Error   LNK2005 __register_onexit_function already defined in ucrtd.lib(ucrtbased.dll)
Error   LNK2005 __seh_filter_dll already defined in ucrtd.lib(ucrtbased.dll)    

1 个答案:

答案 0 :(得分:0)

关于 LNK4042 警告,如果您打开整个项目的属性,并将 C/C++ -> Output Files -> "Object File Name" 下的值更改为以下:

$(IntDir)/%(RelativeDir)/