简体   繁体   English

在Windows中使用g ++构建C ++文件不能用作.bat

[英]Building C++ file with g++ in Windows not working as .bat

Good morning, 早上好,

I am a new user of Windows (+10 years using Linux) and for some reason I have to compile a C++ program. 我是Windows的新用户(使用Linux + 10年),由于某种原因,我必须编译一个C ++程序。 I do not have a big experience in debugging in Windows, but I am eager to understand what I do wrong. 我在Windows中没有很好的调试经验,但我很想知道我做错了什么。

I use MinGW with g++ and different libraries, particularly one called libjpeg. 我使用MinGW和g ++以及不同的库,特别是一个名为libjpeg的库。

When I compile & build the file in a PowerShell using the following command, everything works fine, and my C++ code gives me the expected results. 当我使用以下命令在PowerShell中编译和构建文件时,一切正常,我的C ++代码给了我预期的结果。

g++ -o imageProcessing imageProcessing.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\GnuWin32\include' -L'C:\GnuWin32\lib' -ljpeg

When I use the exact same command in a .bat file, or directly in my C++ editor (geany), I have this error: 当我在.bat文件中使用完全相同的命令,或直接在我的C ++编辑器(geany)中使用时,我有这样的错误:

imageProcessing.cpp:6:21: fatal error: jpeglib.h: No such file or directory
 #include <jpeglib.h>
                     ^
compilation terminated.

It doesn't find the jpeglib.h. 它找不到jpeglib.h。

The correct path were set in the environmental variables. 在环境变量中设置了正确的路径。

The libjpeg I use allows me to open/save jpeg pictures when I use the CImg library. 我使用的libjpeg允许我在使用CImg库时打开/保存jpeg图片。 It can be found here: 在这里能找到它:

Jpeg for Windows Jpeg for Windows

Here is a very simple example: 这是一个非常简单的例子:

#include <iostream>
#include <jpeglib.h>

using namespace std;

int main() {
    float number;
    cout << "Enter a number: "; 
    cin  >> number;
}

Let's call this test.cpp and its location is: C:\\Users\\Rémi\\Documents\\Image_Procesing\\C++\\testCImg 我们称之为test.cpp,其位置为: C:\\Users\\Rémi\\Documents\\Image_Procesing\\C++\\testCImg

Here is what I've added in my environmental variables path: C:\\GnuWin32\\lib;C:\\GnuWin32\\bin;C:\\MinGW\\bin; 这是我在环境变量路径中添加的内容: C:\\GnuWin32\\lib;C:\\GnuWin32\\bin;C:\\MinGW\\bin;

Here is my test.bat: g++ -o test test.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\\GnuWin32\\include' -L'C:\\GnuWin32\\lib' -ljpeg 这是我的test.bat: g++ -o test test.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\\GnuWin32\\include' -L'C:\\GnuWin32\\lib' -ljpeg

test.bat is also located in: C:\\Users\\Rémi\\Documents\\Image_Procesing\\C++\\testCImg test.bat也位于: C:\\Users\\Rémi\\Documents\\Image_Procesing\\C++\\testCImg

the .bat fail is the following: .bat失败如下:

C:\Users\Rémi\Documents\Image_Procesing\C++\testCImg>g++ -o test test.cpp -lgdi32 -Dgmic_use_jpeg -I'C:\GnuWin32\include
' -L'C:\GnuWin32\lib' -ljpeg
test.cpp:3:21: fatal error: jpeglib.h: No such file or directory
 #include <jpeglib.h>
                     ^
compilation terminated.

I spent all my day yesterday trying to figure it out, and I am limited by my Windows knowledge. 我昨天花了整整一天的时间试图解决这个问题,而且我受到Windows知识的限制。 If someone could help me, I would appreciate it very much. 如果有人可以帮助我,我会非常感激。

Thank you. 谢谢。

When you have include path problems, ask GCC to tell you what it's using. 当您包含路径问题时,请让GCC告诉您它正在使用什么。

In the below example, you can clearly see that it's searching for a location 'test' and ignoring it for not existing. 在下面的示例中,您可以清楚地看到它正在搜索位置'test'并忽略它不存在。

You can then deduce that, as mentioned elsewhere, the single quotes are wrong in Batch. 然后,您可以推断,如其他地方所述,Batch中的单引号是错误的。 At that point you'd give "test" a go and achieve success. 那时你要给"test"一个机会并取得成功。

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\lightness>g++ -E -x c++ - -v -I'test'
Using built-in specs.
COLLECT_GCC=g++
Target: mingw32
Configured with: ../../src/gcc-4.7.1/configure --build=mingw32 --enable-language
s=c,c++,ada,fortran,objc,obj-c++ --enable-threads=win32 --enable-libgomp --enabl
e-lto --enable-fully-dynamic-string --enable-libstdcxx-debug --enable-version-sp
ecific-runtime-libs --with-gnu-ld --disable-nls --disable-win32-registry --disab
le-symvers --disable-build-poststage1-with-cxx --disable-werror --prefix=/mingw3
2tdm --with-local-prefix=/mingw32tdm --enable-cxx-flags='-fno-function-sections
-fno-data-sections' --with-pkgversion=tdm-1 --enable-sjlj-exceptions --with-bugu
rl=http://tdm-gcc.tdragon.net/bugs
Thread model: win32
gcc version 4.7.1 (tdm-1)
COLLECT_GCC_OPTIONS='-E' '-v' '-I' ''\''test'\''' '-mtune=i386' '-march=i386'
 c:/mingw32/bin/../libexec/gcc/mingw32/4.7.1/cc1plus.exe -E -quiet -v -I 'test'
-iprefix c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/ - -mtune=i386 -march=i386
ignoring nonexistent directory "c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/../../..
/../mingw32/include"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude/c++"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude/c++/mingw32"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude/c++/backward"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/../
../../../include"
ignoring duplicate directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/inc
lude-fixed"
ignoring nonexistent directory "c:/mingw32/lib/gcc/../../lib/gcc/mingw32/4.7.1/.
./../../../mingw32/include"
ignoring nonexistent directory "'test'"
#include "..." search starts here:
#include <...> search starts here:
 c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include/c++
 c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include/c++/mingw32
 c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include/c++/backward
 c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include
 c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/../../../../include
 c:\mingw32\bin\../lib/gcc/mingw32/4.7.1/include-fixed
End of search list.
# 1 "<stdin>"

Argument tokens in Windows traditionally use the " character. I'm thinking the single quotes are being interpreted as part of the path names, resulting in the "no such file or directory" errors. That would explain why it works in PowerShell, though, as single quotes are valid there. Windows中的参数标记传统上使用"字符。我认为单引号被解释为路径名称的一部分,导致”没有这样的文件或目录“错误。这可以解释为什么它在PowerShell中工作,但是,因为单引号在那里是有效的。

See if you have better luck with this: 看看你运气好吗:

g++ -o imageProcessing imageProcessing.cpp -lgdi32 -Dgmic_use_jpeg -I"C:\GnuWin32\include" -L"C:\GnuWin32\lib" -ljpeg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM