0

I'm trying to include Eclipse Mosquitto library sample mosquitto example from here

I'm trying to use a Makefile to build and compile the code.

I'm facing an issue where the compiler/linker/whatsoever cannot find the mosquitto library located at C:\Program Files\Mosquitto\devel. Here's the error:

mqtt-hostlink> make
gcc -Wall -o main main.c -LC:\\Program ,_,Files\\Mosquitto\\devel\mosquitto.lib
gcc: error: ,_,Files\Mosquitto\devel\mosquitto.lib: No such file or directory
make: *** [Makefile:11: make] Error 1

Here's my Makefile:

CC = gcc
null      :=
SPACE     := $(null) $(null)

LIBS = -LC:\\Program$(SPACE),_,Files\\Mosquitto\\devel\mosquitto.lib

%.o: %.c 
    $(CC) -c -o $@ $< 

make: main.c
    $(CC) -Wall -o main $^ $(LIBS)

.PHONY: clean

The "space" method I referred from : How to escape spaces inside a Makefile

hweiu321
  • 103
  • 4
  • 14
  • What about `LIBS = "C:\\Program Files\\Mosquitto\\devel\\mosquitto.lib"`? The option `-L` is for specifying *directories* that contain library files, not the library file itself. BTW: A Makefile rule `make: main.c` should create a file `make` while your command creates `main`. – Bodo Oct 11 '21 at 10:54
  • @Bodo Sorry I can't understand the last sentence. You mean a file called `make` is created using command `make: main.c`? – hweiu321 Oct 12 '21 at 01:38
  • @hweiu321 -L should be used to specify the search directory for the libraries and -l be used to specify the library to bind, you can try like this LIBS = "C:\\Program Files\\Mosquitto\\devel -lmosquitto", not sure if make will append the .lib, you can also try "LIBS=-l C:\\Program Files\\Mosquitto\\devel\\mosquitto.lib" – Pras Oct 12 '21 at 02:51

2 Answers2

1

You can just use the short name for tar directory. List the directories with /X option as below

C:\>dir /X p*


 Directory of C:\

09/06/2021  02:24 PM    <DIR>          PROGRA~1     Program Files
09/06/2021  01:29 PM    <DIR>          PROGRA~2     Program Files (x86)
               0 File(s)              0 bytes

Then just use PROGRA~1 instead of Program Files

Pras
  • 4,047
  • 10
  • 20
  • It gives this error. I think `make` didn't understand the `~` shorthand. `gcc -Wall -o main main.c "C:\Program~1\Mosquitto\devel\mosquitto.lib" gcc.exe: error: C:\Program~1\Mosquitto\devel\mosquitto.lib: No such file or directory make: *** [Makefile:11: make] Error 1` – hweiu321 Oct 12 '21 at 01:46
1

There's some confusion here. That post is about how to escape spaces from make functions. You are not trying to invoke any make functions here, you are just trying to use a variable in a command line. There's no need to escape anything from make.

What you need to do is escape the spaces from the shell, not from make. You can do that easily, just using quotes. No need for fancy make operations. I recommend you also use forward-slashes, not backslashes. Almost all Windows programs accept both forward and backslashes. Only cmd.exe built-ins don't.

LIBS = "-LC:/Program Files/Mosquitto/devel/mosquitto.lib"
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • But when I run `make`, the compiler still cannot detect the #include header file. – hweiu321 Oct 12 '21 at 01:35
  • @hweiu321 To define a search directory for `#include` you need option `-I`. Assuming you updated your Makefile, please edit your question, **append** the new version and copy&paste both the compile command that gets executed and the full error message(s). – Bodo Oct 12 '21 at 10:54
  • The error you showed us was a linker error. There was no message about a missing header file in your question. – MadScientist Oct 12 '21 at 12:56