1

I got the temp_conversion example program from mosquitto C++, from here. I have looked at this thread but I don't quite get it, I have minimal experience with makefiles. I'm not sure if static libraries even work this way, but I have 2 questions:

  1. Is it possible to be able to move the executable to another pc and have it still run? (without installing mosquitto libraries there, maybe copying a static library created by the makefile with it)

  2. How do I convert this makefile to make a static "moveable" executable?

The makefile:

CFLAGS=-Wall -ggdb -I../../lib -I../../lib/cpp
LDFLAGS=-L../../lib ../../lib/cpp/libmosquittopp.so.1 ../../lib/libmosquitto.so.1

.PHONY: all clean

all : mqtt_temperature_conversion

mqtt_temperature_conversion : main.o temperature_conversion.o
${CXX} $^ -o $@ ${LDFLAGS}

main.o : main.cpp
    ${CXX} -c $^ -o $@ ${CFLAGS}

temperature_conversion.o : temperature_conversion.cpp
    ${CXX} -c $^ -o $@ ${CFLAGS}

clean : 
    -rm -f *.o mqtt_temperature_conversion
tambre
  • 4,625
  • 4
  • 42
  • 55
Weffel
  • 113
  • 1
  • 2
  • 9
  • On which system are you ? Windows or Linux ? – Hugal31 Aug 30 '17 at 12:11
  • linux -> lubuntu 14.04 – Weffel Aug 30 '17 at 12:13
  • You'll need to build mosquitto and all its dependencies into static library (it may or may not support this, also you may or may not violate licensing terms of this library and/or its dependencies) and link it, probably along with linking static C++ standard library. Also resulting executable will be portable only to some degree anyway. – user7860670 Aug 30 '17 at 12:13
  • C != C++. Tag with only one, unless both are used. – tambre Aug 30 '17 at 12:52
  • Why do you link the libraries like that and not link them with the `-l` option? ie `-lmosquitto` – Chris Turner Aug 30 '17 at 13:05
  • @ChrisTurner This is the makefile delivered with the example, I am trying to get the example running like I described, using my limited experience with makefiles. – Weffel Aug 30 '17 at 13:52

2 Answers2

1

1) Yes you can compile on one computer and run it on another if they're running the same OS version. If you don't have the 3rd party libraries installed on the other computer, you will need to compile a static version of the program.

2) To compile a static version of a program you have to add the -static option to the final build command. This instructs the linker to use static versions of libraries if they exist, including the standard C++ library.

This won't work with what you have currently as it requires you to be adding libraries using the -l option. The -l option tells the linker to find a library in the library search path (specified by -L). Your Makefile is adding the libraries by filename, so it'll always use that specific version regardless of any other options you specify.

So to fix that you want something like this

LDFLAGS=-L../../lib -L../../lib/cpp -lmosquittopp -lmosquitto

...which adds the two directories where the libraries live to the search path and then says to link with both libraries. Then if you have static versions of those libraries, it'll use them when you add the -static flag.

Chris Turner
  • 8,082
  • 1
  • 14
  • 18
0

You have to get the static versions of your dependencies, here libmosquittopp. Find or install the static library, for example "libmosquittopp.a" (.a mean static library). After that, link with your executable by replacing libmosquittopp.so.1 and libmosquitto.so.1 with the static version.

Hugal31
  • 1,610
  • 1
  • 14
  • 27
  • 1
    Building with the static libmosquitto may relieve the need to install Mosquitto on another machine, but it won't help with other dependencies that your application may have. If you're running the same Linux distribution and version on all machines, you'll probably get away with it. However, Linux users are generally more comfortable with installing dependencies than are users of most other popular platforms. It probably isn't fatal to the success of your application if users have to do "apt-get install mosquitto" or whatever. – Kevin Boone Aug 30 '17 at 13:44