1

I'm trying to use the Capstone disassembly framework in a C++ project on Ubuntu. I ran the following command to install the development package:

sudo apt-get install libcapstone-dev

This installed the includes in /usr/include/capstone but when I try to compile my application using the #include <capstone/capstone.h> include, I get linker errors:

undefined reference to `cs_open'
undefined reference to `cs_disasm'
undefined reference to `cs_free'
undefined reference to `cs_close'

These are all functions which should be implemented in the libcapstone.so.

Indeed, there is no libcapstone.so in the /usr/lib folder:

ubuntu@DESKTOP:/usr/lib$ ls
accountsservice    emacsen-common         libguestlib.so.0      networkd-dispatcher   software-properties
apt                environment.d          libguestlib.so.0.0.0  open-vm-tools         ssl
bfd-plugins        file                   libhgfs.so.0          openssh               sudo
binfmt-support     gcc                    libhgfs.so.0.0.0      os-release            sysctl.d
binfmt.d           git-core               libpsm1               pkg-config.multiarch  systemd
byobu              gnupg                  libvgauth.so.0        pkgconfig             sysusers.d
clang              gnupg2                 libvgauth.so.0.0.0    pm-utils              tar
cloud-init         gold-ld                libvmtools.so.0       policykit-1           tc
cnf-update-db      groff                  libvmtools.so.0.0.0   python2.7             tmpfiles.d
command-not-found  initcpio               llvm-6.0              python3               ubuntu-release-upgrader
compat-ld          initramfs-tools        locale                python3.6             update-notifier
dbus-1.0           kernel                 lxcfs                 python3.7             valgrind
debug              klibc                  lxd                   rsyslog               x86_64-linux-gnu
dpkg               language-selector      man-db                sasl2
dracut             libDeployPkg.so.0      mime                  sftp-server
eject              libDeployPkg.so.0.0.0  modules-load.d        snapd

sudo apt-get install libcapstone2 does not work since the package is not found.

Is there any other package I need to install or maybe there is some simple CMake find_package() for Capstone? Resources I find do not seem to make the Capstone setup for linking against it/including it in a project straightforward enough.

EDIT:
The installed files by the libcapstone-dev package are:

$ dpkg-query -L libcapstone-dev
/.
/usr
/usr/include
/usr/include/capstone
/usr/include/capstone/arm.h
/usr/include/capstone/arm64.h
/usr/include/capstone/capstone.h
/usr/include/capstone/mips.h
/usr/include/capstone/platform.h
/usr/include/capstone/ppc.h
/usr/include/capstone/sparc.h
/usr/include/capstone/systemz.h
/usr/include/capstone/x86.h
/usr/include/capstone/xcore.h
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libcapstone.a
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/capstone.pc
/usr/share
/usr/share/doc
/usr/share/doc/libcapstone-dev
/usr/share/doc/libcapstone-dev/CREDITS.TXT
/usr/share/doc/libcapstone-dev/HACK.TXT
/usr/share/doc/libcapstone-dev/README
/usr/share/doc/libcapstone-dev/TODO
/usr/share/doc/libcapstone-dev/copyright
/usr/lib/x86_64-linux-gnu/libcapstone.so
/usr/share/doc/libcapstone-dev/changelog.Debian.gz
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
  • 1
    "Indeed, there is no `libcapstone.so` in the `/usr/lib` folder." - Eh? Ubuntu package `libcapstone-dev` [contains](https://packages.ubuntu.com/ru/xenial/amd64/libcapstone-dev/filelist) file `/usr/lib/libcapstone.so`. – Tsyvarev Oct 22 '19 at 08:18
  • @Tsyvarev: I added my directory listing and `libcapstone.so` actually didn't get installed. I'm using `Ubuntu 18.04.1` via `WSL`. – BullyWiiPlaza Oct 22 '19 at 11:42
  • 1
    Strange, have you checked what **actually** has been installed as a part of `libcapstone-dev` package? See [that question](https://askubuntu.com/questions/32507/how-do-i-get-a-list-of-installed-files-from-a-package) about ways for doing this. – Tsyvarev Oct 22 '19 at 12:20
  • 1
    And please, "I get a linker error" is not a very useful description without the error message itself... – Tsyvarev Oct 22 '19 at 12:21
  • @Tsyvarev: Thank you very much! Due to your tip I found that `/usr/lib/x86_64-linux-gnu/libcapstone.so` is the file path. Adding `target_link_libraries(${PROJECT_NAME} "/usr/lib/x86_64-linux-gnu/libcapstone.so")` to the `CMake` file causes `Capstone` to link correctly (but it's not a pretty solution, yet). – BullyWiiPlaza Oct 22 '19 at 12:59
  • 1
    Hm, aren't `/usr/lib/x86_64-linux-gnu` is searched by the linker by default? That is, `target_link_libraries(project_name capstone)` should be sufficient. – Tsyvarev Oct 22 '19 at 13:02
  • @Tsyvarev: Yes, that works as well :) – BullyWiiPlaza Oct 22 '19 at 13:05
  • If this issue is resolved, please consider writing up a quick answer post (and mark as accepted) so others with the same issue can understand what the solution is. Thanks! – Kevin Oct 22 '19 at 13:16

1 Answers1

1

Capstone seems to provide pkg-config .pc file. So, the following should work:

include(FindPkgConfig)

pkg_check_modules (CAPSTONE REQUIRED capstone)

# Use CAPSTONE_FOUND, CAPSTONE_LIBRARIES, CAPSTONE_INCLUDE_DIRS vars
target_link_libraries(${PROJECT_NAME} ${CAPSTONE_LIBRARIES})
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
arrowd
  • 33,231
  • 8
  • 79
  • 110