0

I'm facing some issues when linking my c++ program, here is what I do : g++ tp.cpp -llpsolve55 -lcolamd -ldl -o MyExe and the command-line return me this :

/usr/bin/ld: cannot find -llpsolve collect2: error: ld returned 1 exit status

But I've already installed lpsolve, it appear in Synapatic as installed and I even installed it via the terminal

  • 1
    may be you should put lpsolve on the Path – Eduardo Pascual Aseff Feb 15 '20 at 23:55
  • How can I do that ? –  Feb 15 '20 at 23:57
  • 1
    it depends of your operative system – Eduardo Pascual Aseff Feb 15 '20 at 23:59
  • my OS is Ubuntu Mate –  Feb 16 '20 at 00:00
  • I solved this problem, now I'm trying to run MyExe (file) and I get this : ./MyExe: error while loading shared libraries: liblpsolve55.so: cannot open shared object file: No such file or directory –  Feb 16 '20 at 00:05
  • 2
    Put the directory with the library into the LD_LIBRARY_PATH environment variable. – Sam Varshavchik Feb 16 '20 at 00:06
  • You've not included the relevant portions of the error code, which explain why `ld` failed. It's in the line or two before the one you posted. It's most likely an unresolved external issue, but it's impossible to help without knowing the rest of the error message. – Ken White Feb 16 '20 at 00:06
  • To solve my previous problem I put this absolute path of lpsolve api : g++ tp.cpp /usr/lib/lp_solve/liblpsolve55.so -lcolamd -ldl -o MyExe –  Feb 16 '20 at 00:06
  • I put all my message error --> ./MyExe: error while loading shared libraries: liblpsolve55.so: cannot open shared object file: No such file or directory I don't understand –  Feb 16 '20 at 00:12
  • Are you using `dlopen()` in your program too? I'm wondering just because I noticed you link with the `dl` library. You don't need that if you link with the library. – Ted Lyngmo Feb 16 '20 at 00:49

1 Answers1

2

If /usr/lib/lp_solve is not in the normal search path for libraries, you can add that path to your executeable when linking. Also note that libraries should typically come last:

g++ -o MyExe tp.cpp -L /usr/lib/lp_solve  -Wl,-rpath,/usr/lib/lp_solve -llpsolve55 -lcolamd

The -L argument adds the directory to the list of directories to look for libraries in when doing the linking.

-Wl tells the compiler to pass on what follows to the linker.

The linkers -rpath,<path> argument tells it to add <path> to MyExe so it can find the library when you later run the program.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Even by running this command I still get the same error that I had : ./MyExe: error while loading shared libraries: liblpsolve55.so: cannot open shared object file: No such file or directory –  Feb 16 '20 at 00:29
  • Perfect ! it worked, thanks, what was the problem ...? –  Feb 16 '20 at 00:55
  • You are welcome! I added an extra `/lib` after `ip_solve` in my previous version. Bad eyesight. :-) Note that you don't need `-ldl` unless you use the `dlopen()` family of functions. – Ted Lyngmo Feb 16 '20 at 00:55
  • Oh okay haha don't worry. And what is the purpose of the -L argument, and -W1 ? –  Feb 16 '20 at 01:05
  • @ADL92 Added explanation (and I removed `-ldl` from the command line since I assume that you don't use `dlopen()`). – Ted Lyngmo Feb 16 '20 at 01:14