【问题标题】:Compiling C++ with C codes on VS Code using G++ compiler使用 G++ 编译器在 VS Code 上使用 C 代码编译 C++
【发布时间】:2022-01-09 08:14:44
【问题描述】:

我的情况如下: 我在 Microsoft Visual Studio 上制作了一个 GUI 程序(基于 wxWidgets),该代码在混合 C++ 和 C 文件时运行良好,基本上 C 文件做的事情很少,但我不能丢弃它们,因为我是 C++ OOP 的初学者 -我有 Java 的 OOP 背景,我觉得这与 C++ 大不相同——但我可以在一定程度上处理它。无论如何,该程序在 Visual Studio 2019 上运行良好,但是当我将它放在另一台没有安装 Visual Studio 的计算机上时,它开始触发缺少的 .dlls 经过很少的研究我发现这是由于缺少 Visual Studio 安装我没有管理员权限来安装这些.dll 文件。我在 VS Code 上寻找重建文件,我使用了 g++ 编译器,所以我需要使用 MinGw 编译器配置 wxWidgets。我设法使 wxWidgets 上的 Hello World 代码工作。当我运行我的代码时,经过一番挣扎后它没有工作,我消除了任何错误,但是当我为 Debug/Release 构建代码时,它在终端上得到了什么:

C:\Users\AppData\Local\Temp\ccUB3ReN.o:在函数中 ZN5cMainC2Ev: C:/Users//Desktop/wxTest/src/cMain.cpp:13: 未定义对Bus_Construct 的引用 C:/Users//Desktop/wxTest/src/cMain.cpp:13: 未定义引用 到Decker_Construct C:\Users\AppData\Local\Temp\ccUB3ReN.o: 在函数ZN5cMain16CalculateClickedER14wxCommandEvent: C:/Users//Desktop/wxTest/src/cMain.cpp:426: 未定义参考 到getCG C:/Users//Desktop/wxTest/src/cMain.cpp:442: 未定义 参考getCGz C:/Users//Desktop/wxTest/src/cMain.cpp:454: 未定义参考 到getPassCGX C:/Users//Desktop/wxTest/src/cMain.cpp:455: 未定义对getPassCGY 的引用 C:/Users//Desktop/wxTest/src/cMain.cpp:456: 未定义参考 到getPassCGZ C:/Users//Desktop/wxTest/src/cMain.cpp:457: 未定义对totalCG 的引用 C:/Users//Desktop/wxTest/src/cMain.cpp:470: 未定义引用 到delay C:/Users//Desktop/wxTest/src/cMain.cpp:528: 未定义 参考freeSeats C:/Users//Desktop/wxTest/src/cMain.cpp:530: 未定义参考 到freeSingleDecker C:\Users\AppData\Local\Temp\ccUB3ReN.o: 在函数ZN5cMain19GenerateGroupsClickER14wxCommandEvent 中: C:/Users//Desktop/wxTest/src/cMain.cpp:622: 未定义引用 到initSingleDecker C:/Users//Desktop/wxTest/src/cMain.cpp:627: 未定义参考 toinitSeatscollect2.exe:错误:ld返回1退出状态 终端进程 “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -命令 C:\Softwares\MinGW\mingw32\bin\g++.exe -g C:\Users\Desktop\wxTest\src*.cpp C:\Users\Desktop\wxTest\src*.c -o C:\Users\Desktop\wxTest\build\debug\cApp.exe -I C:\Users\Desktop\wxTest\include -I C:\wx\wxWidgets-3.1.5\include -I C:\wx\wxWidgets-3.1.5\lib\gcc_dll\mswud -L C:\wx\wxWidgets-3.1.5\lib\gcc_dll -l wxmsw31ud_core -l wxbase31ud" 以退出代码终止:1

我注意到的是一些带有ZN5cMainC2Ev 的重命名名称,它最初是一个类名cMain。 我认为诀窍在于task.json 参数:

{
"version": "2.0.0",
"tasks": [
    {
        "type": "shell",
        "label": "Debug",
        "command": "C:\\Amir\\Softwares\\MinGW\\mingw32\\bin\\g++.exe",
        "args": [
            "-g",
            "${workspaceFolder}\\src\\*.cpp",
            "-o",
            "${workspaceFolder}\\build\\debug\\${fileBasenameNoExtension}.exe",
            "-I",
            "${workspaceFolder}\\include",
            "-I",
            "C:\\wx\\wxWidgets-3.1.5\\include",
            "-I",
            "C:\\wx\\wxWidgets-3.1.5\\lib\\gcc_dll\\mswud",
            "-L",
            "C:\\wx\\wxWidgets-3.1.5\\lib\\gcc_dll",
            "-l",
            "wxmsw31ud_core",
            "-l",
            "wxbase31ud",
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "compiler: C:\\Amir\\Softwares\\MinGW\\mingw32\\bin\\g++.exe"
    },
    {
        "type": "shell",
        "label": "Release",
        "command": "C:\\Amir\\Softwares\\MinGW\\mingw32\\bin\\g++.exe",
        "args": [
            "${workspaceFolder}\\src\\*.cpp",
            "${workspaceFolder}\\src\\BusCalcs.c",
            "${workspaceFolder}\\src\\CG_Engine.c",
            "${workspaceFolder}\\src\\Helper.c",
            "-o",
            "${workspaceFolder}\\build\\release\\${fileBasenameNoExtension}.exe",
            "-I",
            "${workspaceFolder}\\include",
            "-I",
            "C:\\wx\\wxWidgets-3.1.5\\include",
            "-I",
            "C:\\wx\\wxWidgets-3.1.5\\lib\\gcc_dll\\mswu",
            "-L",
            "C:\\wx\\wxWidgets-3.1.5\\lib\\gcc_dll",
            "-l",
            "wxmsw31u_core",
            "-l",
            "wxbase31u",
        ],
        "options": {
            "cwd": "${workspaceFolder}"
        },
        "problemMatcher": [
            "$gcc"
        ],
        "group": "build",
        "detail": "compiler: C:\\Amir\\Softwares\\MinGW\\mingw32\\bin\\g++.exe"
    }
]

}

【问题讨论】:

  • 您是否在 C 位的 C++ 声明中使用了extern "C"
  • "C 文件做的事情很少,但我不能丢弃它们,因为我是 C++ OOP 的初学者" -- 你明白你没有在 C++ 中使用 OOP?
  • 不要使用 C++ 编译器(例如 g++)编译 C 源文件,您似乎已将项目配置为这样做。至少在发布模式下。在调试模式下,您似乎正在编译一组较小的文件。使用 C 编译器 (gcc) 编译 C,使用 C++ 编译器编译 C++。
  • @FredLarson 是的,.h C 文件位于外部“C”范围/块内
  • @JohnBollinger 我对“制作”文件或换句话说这些编译器和链接器配置文件/脚本完全陌生。那么在task.json中写什么来用gcc编译器编译这些C文件而不是g ++,它们会全部链接到1个.exe文件中吗?顺便说一句,我希望所有 cpp 和 c 文件在一天结束时都编译并链接在一起,用于调试和发布

标签: c++ c visual-studio-code linker g++


【解决方案1】:

通过将.C 文件重命名为.CPP 文件解决了这个问题,它为调试和发布正确构建,我的应用程序工作正常,但是当我在另一台计算机上运行它时,我发现大量缺少.dlls比我以前从 VS2019 构建中获得的更多我认为我现在的问题是处理如何优化我在任何计算机上的应用程序安装。

谢谢大家。

【讨论】:

    猜你喜欢
    • 2021-01-03
    • 2015-11-05
    • 2020-03-07
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    相关资源
    最近更新 更多