[英]C++ library in Visual Studio Code
I have made a c++ project in CodeBlocks.我在 CodeBlocks 中做了一个 c++ 项目。 Now I started using Visual Studio Code and I can't figure out how to connect files.现在我开始使用 Visual Studio Code,但我不知道如何连接文件。 I have main.cpp
with my main code, then I have sortlib.cpp
with some sorting functions and I have sortlib.h
.我有main.cpp
和我的主要代码,然后我有sortlib.cpp
和一些排序功能,我有sortlib.h
。 My question is how do I connect these files, so I can #include "sortlib.h"
in my main.cpp
file and use the functions.我的问题是如何连接这些文件,以便我可以在main.cpp
文件中#include "sortlib.h"
并使用这些函数。 I have tried including sortlib.h
in my sortlib.cpp
program and then my sortlib.h
in my main.cpp
program, but it didn't work.我尝试在我的sortlib.cpp
程序中包含sortlib.h
,然后在我的main.cpp
程序中包含我的sortlib.h
,但它没有用。
I'm on windows so I just compiled my sortlib.cpp
program by typing g++ sortlib.cpp
and it showed me this error message c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to 'WinMain@16'
I'm on windows so I just compiled my sortlib.cpp
program by typing g++ sortlib.cpp
and it showed me this error message c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):(.text.startup+0xc0): undefined reference to 'WinMain@16'
main.cpp
// Other header files
#include "sortlib.h"
int main(void) {
...
}
sortlib.h
void someFunction();
...
sortlib.cpp
// Include headers that are required
void someFunction() {
...
}
g++ -g main.cpp sortlib.cpp
There is no such thing as "connecting" files in programming.编程中没有“连接”文件之类的东西。 You have to make sure that your compiler is compiling all the.cpp files into.o/.obj files and that they are all getting linked together to produce your final executable.您必须确保您的编译器将所有 .cpp 文件编译为 .o/.obj 文件,并且它们都链接在一起以生成最终的可执行文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.