外部入口点C++(LIB或DLL)

外部入口点C++(LIB或DLL),c++,extern,entry-point,C++,Extern,Entry Point,我想知道是否有可能在某种库中创建入口点main或winmain。我正在尝试编写一个窗口管理器代码,我希望在库中包含main函数,其中包含特定于应用程序的文件,这些文件只定义一些由winmain调用的外部函数,例如extern render或extern refresh 我自己尝试过这样做,但是我遇到了一个错误,没有定义入口点。您可以在项目中使用指定的从DLL导出 我只是花了最后几天的时间试图为自己弄明白这一点,结果运气不错 注意,我只在static libraries.lib中尝试了这个方法 l

我想知道是否有可能在某种库中创建入口点main或winmain。我正在尝试编写一个窗口管理器代码,我希望在库中包含main函数,其中包含特定于应用程序的文件,这些文件只定义一些由winmain调用的外部函数,例如extern render或extern refresh


我自己尝试过这样做,但是我遇到了一个错误,没有定义入口点。

您可以在项目中使用指定的从DLL导出

我只是花了最后几天的时间试图为自己弄明白这一点,结果运气不错

注意,我只在static libraries.lib中尝试了这个方法

lib文件的问题是,只有在调用库的函数时,它们才会被使用并连接到项目。 现在,最小项目的问题是,您将只有一个主功能。 但这不是你的项目所要求的,那么它是如何与之联系的呢

我的解决方案可能没有那么优雅,但对我来说很有效: 创建一个LibConnection.h,其中包含lib.h并从lib.cpp调用一个伪函数。 在我看来,不好的部分是必须将lib.h和Connectionlib.h包含到项目文件中

像这样:

//Lib.h
void ConnectionFunction();

//Lib.cpp
int main(int argc, char* argv[])
{
    //do some stuff
}

//This function doesn't do anything but it is important 
//that you define it in your lib.h and declare it in your lib.cpp
void ConnectionFunction()
{
}
//LibConnection.h
#include "Lib.h"
//now we call the connectionfunction
//remember non of this get really called but it makes possible connecting with your
//almost empty library
void Dummy()
{
     ConnectionFunction();
}
现在您有了一个基本的库,并且必须创建一个连接文件 像这样:

//Lib.h
void ConnectionFunction();

//Lib.cpp
int main(int argc, char* argv[])
{
    //do some stuff
}

//This function doesn't do anything but it is important 
//that you define it in your lib.h and declare it in your lib.cpp
void ConnectionFunction()
{
}
//LibConnection.h
#include "Lib.h"
//now we call the connectionfunction
//remember non of this get really called but it makes possible connecting with your
//almost empty library
void Dummy()
{
     ConnectionFunction();
}
然后在空项目中:

//testapp.cpp
#include "LibConnection.h"
//remember to include the lib.h and the libconnection.h into your project files

void Foo()
{
    //this function doesn't get called but your project is running!
}
希望这有帮助:

可能重复的