Abhishek Singh
Abhishek Singh

Reputation: 83

Error while loading shared libraries: libcmocka.so.0: No such file or directory

#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?

Upvotes: 6

Views: 5047

Answers (3)

smac89
smac89

Reputation: 43098

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

Upvotes: 0

Pradheep
Pradheep

Reputation: 3819

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

Upvotes: 1

Philips George John
Philips George John

Reputation: 416

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.

Upvotes: 0

Related Questions