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

user2505778

I'm beginning to teach myself a bit of C++ these days off, and I decided to learn some GUI programming in the meantime. Thing is, it just won't compile, either using GTK (Windows bundle 3.6.4) or wxWidgets (3.0.0). The error I get is similar for both, so I'm sticking with GTK+ for this question.

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):

@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):

: Invalid argument
: Invalid argument

I tried the same without Cygwin at first, but the results were:

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

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.

Also, I changed the compiler to TDM-GCC because I couldn't compile wx with MinGW. 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++. 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.)

kfsone

This is NOT a gcc issue. This is a scripting/shell/bash/environment issue.

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.

That means that the shell did not consume $(pkg-config --cflags gtk+-3.0) and perform substitution on it.

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.

#!/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

Change your bat file to invoke:

%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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related