简体   繁体   English

为什么g ++不能编译简单的GTK程序?

[英]Why won't g++ compile a simple GTK program?

I'm beginning to teach myself a bit of C++ these days off, and I decided to learn some GUI programming in the meantime. 这些天我开始教自己一些C ++,我决定在此期间学习一些GUI编程。 Thing is, it just won't compile, either using GTK (Windows bundle 3.6.4) or wxWidgets (3.0.0). 事实是,它只是不会编译,使用GTK(Windows捆绑3.6.4)或wxWidgets(3.0.0)。 The error I get is similar for both, so I'm sticking with GTK+ for this question. 我得到的错误与两者相似,所以我坚持使用GTK +来解决这个问题。

After 5 days (7 recompiles of wx, many tutorials, SO questions and google), this is how I'm trying to compile the code (I'm under Windows 7, 64 bits, and it's not an option to switch to Linux right now): 5天后(7次重新编译wx,很多教程,SO问题和谷歌),这就是我试图编译代码的方式 (我是在Windows 7,64位下,而不是选择切换到Linux吧现在):

@echo off

set cygpath=C:\Cygwin\bin
set filename=simple

%cygpath%\bash.exe -c "gcc $(pkg-config --cflags gtk+-3.0) -o %filename% %filename%.c $(pkg-config --libs gtk+-3.0)"

echo.
pause

But the only output I get is this (backtilts instead of $() returns the same thing): 但我得到的唯一输出是(backtilts而不是$()返回相同的东西):

: Invalid argument
: Invalid argument

I tried the same without Cygwin at first, but the results were: 我一开始尝试了没有Cygwin,但结果如下:

gcc: error: $(pkg-config: No such file or directory
gcc: error: gtk+-3.0): No such file or directory
gcc: error: $(pkg-config: No such file or directory
gcc: error: gtk+-3.0): No such file or directory
gcc: error: unrecognized command line option '--cflags'
gcc: error: unrecognized command line option '--libs'

Using quotes or double quotes didn't help at all. 使用引号或双引号根本没有帮助。 I tried all these in both cygwin-bash or Windwos prompt 我在cygwin-bash或Windwos提示符下尝试了所有这些

I'm totally new to C++, wxWidgets and GTK+ and I already saw this , but since it didn't solve my problem and I can't comment because of reputation, I thought I had to open a new question. 我完全不熟悉C ++,wxWidgets和GTK +,我已经看到了这个 ,但由于它没有解决我的问题而我因为声誉而无法评论,我认为我不得不打开一个新问题。

Also, I changed the compiler to TDM-GCC because I couldn't compile wx with MinGW. 此外,我将编译器更改为TDM-GCC,因为我无法使用MinGW编译wx。 Is this somewhat related? 这有点相关吗? What am I missing here? 我在这里错过了什么?

Thanks in advance for any help. 在此先感谢您的帮助。

EDIT : Sorry, just realized that the sample was written in C, not C++. 编辑 :对不起,刚才意识到样本是用C语言编写的,而不是用C ++编写的。 I'm adding the tag now. 我现在正在添加标签。 (However, the error is the same with the C++ code I'm using for wx, so I'm keeping the C++ tag.) (但是,错误与我用于wx的C ++代码相同,所以我保留了C ++标记。)

This is NOT a gcc issue. 这不是gcc问题。 This is a scripting/shell/bash/environment issue. 这是脚本/ shell / bash /环境问题。

gcc: error: $(pkg-config: No such file or directory

This line denotes that 'gcc' saw $(pkg-config where it was expecting a file name; Linux apps expect command-line substitution to have been done for them by the shell invoking them. 这一行表示'gcc'看到了$(pkg-config ,它期待一个文件名; Linux应用程序期望命令行替换是由shell调用它们完成的。

That means that the shell did not consume $(pkg-config --cflags gtk+-3.0) and perform substitution on it. 这意味着shell没有消耗$(pkg-config --cflags gtk+-3.0)并对其执行替换。

You might want to try making a bash script, gtkcc.sh, which does this for you and inserts the relevant parameters in the appropriate places. 您可能想尝试制作一个bash脚本gtkcc.sh,它会为您执行此操作并在相应位置插入相关参数。

#!/bin/bash

# usage: gtkcc.sh <filename>
filename="$1"; shift
if [ -z "${filename}" ]; then echo "Usage: $0 <filename>"; exit 255; fi
if [ ! -f "${filename}.c" ]; then echo "Cannot find ${filename}.c"; exit 255; fi

gtkCFLAGS="$(pkg-config --cflags gtk+-3.0)"
gtkLDFLAGS="$(pkg-config --libs gtk+-3.0)"

cmd="gcc ${gtkCFLAGS} -o \"${filename}\" \"${filename}.c\" ${gtkLDFLAGS}"
echo $cmd >${TMP}/gtkcc.log
eval $cmd

Save as gtkcc.sh or gtkcc, chmod a+rx gtkcc.sh 保存为gtkcc.sh或gtkcc, chmod a+rx gtkcc.sh

Change your bat file to invoke: 更改您的bat文件以调用:

%cygpath%\bash.exe gtkcc.sh %filename%

If it doesn't work, start a bash and consult ${TMP}/gtkcc.log, otherwise you can comment out the echo line. 如果它不起作用,启动bash并咨询$ {TMP} /gtkcc.log,否则你可以注释掉回声线。

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

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