14

I wanted to test out Pocketsphinx in Node.JS. It says I need to install Swig version 3.0.7 or above.

I think I installed all the other dependencies correctly. I can even type Swig commands in the Terminal now, but I keep getting this error whenever I run npm install pocketsphinx:

CMake Error at /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:148 (message):
  Could NOT find SWIG (missing: SWIG_DIR) (Required is at least version
  "3.0.7")
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:388 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.6.3/share/cmake/Modules/FindSWIG.cmake:75 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:4 (find_package)

I tried brew install swig, npm install swig, and npm install -g swig. I tried going to the swig download page and following the installation instructions, but nothing I seem to do stops the error from happening. I'm trying this on a Macbook by the way.

I really have no clue what I'm doing here. I just wanted to test out Pocketsphinx and now I've installed Swig in 4 different places, and CMake can't seem to recognise any of them.

Any help would be wonderful!

MysteryPancake
  • 1,365
  • 1
  • 18
  • 47

3 Answers3

10

Came here looking for the Windows-based error. I found a solution that seems to work for me, so decided to post it here.

  • Create two environment variables in the "System Variables" section: SWIG_DIR and SWIG_EXECUTABLE. These must point to /path/to/the/swig/dir/ and /path/to/the/swig/dir/swig.exe respectively.
  • After this, add one more entry to the PATH variable: /path/to/the/swig/dir. Test this out by typing swig in the command prompt. It should display a message must specify an input file. Use -help for available options.
  • Restart the computer to apply all environment variable changes. find_package(SWIG required) should work correctly now.
Lycanthropeus
  • 195
  • 3
  • 17
  • As for the last step, a restart of the IDE shall be enough. I tried your steps and restart my CLion rather than my computer as a whole. It just works. – Eric Jul 02 '21 at 09:48
  • 1
    Note that for me, this did NOT work. You only have to provide `SWIG_EXECUTABLE`. CMake can derive `SWIG_DIR` from `SWIG_EXECUTABLE`, and additionally `SWIG_DIR` should point to the directory that contains the swig libraries, which is usually `/path/to/swig/dir/Lib`. If you get errors like `error : Unable to find 'swig.swg'`, try to remove the `SWIG_DIR` variable. – Verhoevenv Sep 28 '21 at 18:59
  • Consider code-highlighting `PATH`, for clarity. Also, this works in debian/ubuntu – user27221 Jan 04 '22 at 14:21
6

Check the source code for the FindSwig.cmake.

Unfortunately, if a find script does not work as expected and you do not see right away why that is the case, you usually have to dig into its source. In your case, it looks like CMake was able to find and run the SWIG executable, but then failed to obtain the swig directory.

Try manually running swig -swiglib and check that the printed directory indeed contains a swig.swg file. Also, be sure that the swig executable found by CMake is actually the correct one (you can verify this by inspecting the value of SWIG_EXECUTABLE in either the cmake-gui, the ccmake curses interface, or in the CMakeCache.txt file directly).

Note that CMake will not update the executable path once it has been found! So if you make changes to your system that influence the executable location, you will have to clear the cache (eg. by deleting the CMakeCache.txt) and re-run CMake for the changes to take effect.

ComicSansMS
  • 51,484
  • 14
  • 155
  • 166
  • `swig -swiglib` prints `/usr/local/share/swig/3.0.12`. This directory indeed contains `swig.swg`. I still don't quite understand how `FindSwig.cmake` works. I don't know where it's trying to search. Sorry, I'm very stupid which is why I'm using Node.JS – MysteryPancake Jan 23 '18 at 10:21
  • @MysteryPancake Did you check the value of `SWIG_EXECUTABLE` in the `CMakeCache.txt`? This file is located in your project's binary directory, ie. the directory in your project in which you also run CMake. And don't worry, everyone feels stupid when they start using CMake :P – ComicSansMS Jan 23 '18 at 11:02
  • I can't even seem to find `CMakeCache.txt` anywhere. The reason I feel stupid is because I literally have no clue, I don't even know what CMake is or what it does. All I know is Node.JS and some other useless garbage. I guess I set CMake up wrong if that file is missing. Sorry for this stupid problem – MysteryPancake Jan 23 '18 at 11:22
  • CMakeCache.txt is inside build subfolder of node-pocketsphinx – Nikolay Shmyrev Jan 25 '18 at 15:49
1

I tried to work it out for myself. The problem was when I typed npm install swig.

I forgot to set up this version of Swig, and the compiler was using it.

I typed sudo npm uninstall swig and it worked perfectly. I feel very stupid!

MysteryPancake
  • 1,365
  • 1
  • 18
  • 47