6
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
/* A test case that does nothing and succeeds. */
static void null_test_success(void **state) {
    (void) state; /* unused */
}
int main(void) {
    const UnitTest tests[] = {
        unit_test(null_test_success),
    };
    return run_tests(tests);
}

I am new to cmocka unit testing framework, http://www.ohloh.net/p/cmocka. When I compiled the above program as gcc program.c -lcmocka and when i ran ./a.out I got the error:

./a.out:error while loading shared libraries: libcmocka.so.0: cannot open shared object file: No such file or directory

I tried but cannot fix it. What exactly is the problem here?

Joseph Quinsey
  • 9,553
  • 10
  • 54
  • 77

3 Answers3

1

can you check if you have permission to access the folder /usr/local/lib/

Do a ls -lart /usr/local/lib/libcmocka.so and check for the access permission and check if you have read permission

Pradheep
  • 3,553
  • 1
  • 27
  • 35
0

This error means that the program loader cannot find the cmocka shared library file. You need to add the directory in which the shared library (say libmocka.so.x) is present to the file "/etc/ld.so.conf". Including it in the LD_LIBRARY_PATH variable will also work.

Actually it is better to install the libraries (shared and static) to "standard" folders like /usr/lib or /usr/local/lib unless you have some specific reason not to do so.

  • Here is the output for the installation which I did. `abhishek@abhishek-ubuntu:~/Pictures$ sudo make install -- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE) -- Configuring done -- Generating done -- Build files have been written to: /home/abhishek/Pictures [100%] Built target cmocka_shared Install the project... -- Install configuration: "" -- Up-to-date: /usr/local/include/cmocka.h -- Up-to-date: /usr/local/lib/libcmocka.so.0.0.1 -- Up-to-date: /usr/local/lib/libcmocka.so.0 -- Up-to-date: /usr/local/lib/libcmocka.so` – Abhishek Singh Feb 23 '13 at 16:35
  • Is /usr/local/lib in /etc/ld.so.conf? Some googling tells me that Fedora distros do not add that. Why I do not know! Since it is a very common install location for libs. – Philips George John Feb 23 '13 at 16:43
  • 1
    First check if /usr/local/lib is in LD_LIBRARY_PATH and try temporarily adding it using "export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}" before editing /etc/ld.so.conf. Also, please note that some distros have an /etc/ld.so.conf.d directory with .conf files inside that. If so you need to add /usr/local/lib to a new .conf file (say local.conf) inside this directory. After making any changes (to /etc/ld.so.conf or /etc/ld.so.conf.d), you need to run "ldconfig" command. – Philips George John Feb 23 '13 at 16:57
  • thanks! export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH} really worked. Well /usr/local/lib was already present in /etc/ld.so.conf.d/libc.conf so when I ran "sudo ldconfig" it worked :) – Abhishek Singh Feb 23 '13 at 17:20
  • Strangely, on Ubuntu /usr/local/lib is in some file in /etc/ld.so.conf.d/, and the only entry in /etc/ld.so.conf is "include ld.so.conf.d/*.conf", so one would think that /usr/local/lib would be included, but I can only get passed the loader error if I explicitly set LD_LIBRARY_PATH=/usr/local/lib... what gives? – weberc2 Feb 04 '15 at 05:11
  • @weberc2: you need to run `ldconfig` as root to update the cache. See: https://stackoverflow.com/a/18326265/480764 – Dan Barowy Oct 17 '17 at 17:59
  • I had the same problem but I already knew that everything was set correctly in terms of paths etc. So after a few minutes of irritation of knowing the library was there I suddenly remembered "ldconfig".So I issued "sudo ldconfig" and lo and behold no more errors. Hope that helps. – Emacs The Viking Aug 26 '19 at 09:35
0

I use cmake in my project, so the way I solved this was to use the following cmake commands:

# Find and add the cmocka library
find_library(CMOCKA_LIBRARY NAMES cmocka)
add_library(cmocka SHARED IMPORTED)
set_property(TARGET cmocka PROPERTY IMPORTED_LOCATION "${CMOCKA_LIBRARY}")

# Create and link the testing file to cmocka
add_executable(mytest my_example_test.c)
target_link_libraries(mytest cmocka)

# Add this as a test for ctest
add_test(TEST_MY_EXAMPLE mytest)

OR with the command line:

gcc my_example_test.c -L/usr/local/lib -lcmocka -o mytest && ./mytest

In the last case, you can use the -L option to tell gcc where to look for library files. In this case, if you have installed it somewhere unconventional, gcc can still find it if you specify where the library files are located

smac89
  • 39,374
  • 15
  • 132
  • 179