using cmake, gTest and OpenCV: 'undefined reference to' - c++

I tried to compile project consist of GTest, OpenCV, CMake and Make(It is only a script). The hierarchy is:
.
├── build
├── include
│   ├── csv_loader.h
│   ├── loader.h
│   ├── mat_reader.h
│   ├── processor.h
│   └── reader.h
├── Makefile
├── pictures
│   └── pila_original.jpg
├── sources
│   ├── CMakeLists.txt
│   ├── csv_loader.cpp
│   ├── mat_reader.cpp
│   └── processor.cpp
└── test
├── CMakeLists.txt
├── hello-test.cpp
└── not_null_data-test.cpp
When I tried to compile whole project, I got this error in test/CmakeLists.txt:
CMakeFiles/CPOO-PROJECT-TEST.dir/not_null_data-test.cpp.o: In function `NOTNULL_flowtest_Test::TestBody()':
not_null_data-test.cpp:(.text+0x1e): undefined reference to `Processor::Processor()'
not_null_data-test.cpp:(.text+0x2d): undefined reference to `Processor::Detect()'
not_null_data-test.cpp:(.text+0xfb): undefined reference to `Processor::~Processor()'
not_null_data-test.cpp:(.text+0x145): undefined reference to `Processor::~Processor()'
collect2: error: ld returned 1 exit status
./test/CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(CPOO-PROJECT-TEST)
find_package( OpenCV 3.1.0 REQUIRED )
#eanble_testing()
find_package( GTest REQUIRED )
add_definitions(-std=c++11)
set (LIB ../release/libCPOO-PROJECT.so)
file(GLOB TEST "*.cpp")
include_directories(../include)
include_directories( ${OpenCV_INCLUDE_DIRS}
${GTest_INCLUDE_DIRS})
add_executable( ${PROJECT_NAME} ${LIB} ${TEST})
target_link_libraries( ${PROJECT_NAME}
${GTEST_BOTH_LIBRARIES}
pthread
)
I don't know how to fix linker problem, if there is any additional information needed, I'll add them.
EDIT 1
After adding flag to CMake, I get this log:
Scanning dependencies of target CPOO-PROJECT-TEST
make[3]: Leaving directory '/home/mateusz/Projects/EiTI/CPOO/build/test'
make -f CMakeFiles/CPOO-PROJECT-TEST.dir/build.make CMakeFiles/CPOO-PROJECT-TEST.dir/build
make[3]: Entering directory '/home/mateusz/Projects/EiTI/CPOO/build/test'
[ 33%] Building CXX object CMakeFiles/CPOO-PROJECT-TEST.dir/hello-test.cpp.o
/usr/bin/c++ -I/home/mateusz/Projects/EiTI/CPOO/test/../include -I/usr/include/opencv -std=c++11 -o CMakeFiles/CPOO-PROJECT-TEST.dir/hello-test.cpp.o -c /home/mateusz/Projects/EiTI/CPOO/test/hello-test.cpp
[ 66%] Building CXX object CMakeFiles/CPOO-PROJECT-TEST.dir/not_null_data-test.cpp.o
/usr/bin/c++ -I/home/mateusz/Projects/EiTI/CPOO/test/../include -I/usr/include/opencv -std=c++11 -o CMakeFiles/CPOO-PROJECT-TEST.dir/not_null_data-test.cpp.o -c /home/mateusz/Projects/EiTI/CPOO/test/not_null_data-test.cpp
[100%] Linking CXX executable CPOO-PROJECT-TEST
/usr/bin/cmake -E cmake_link_script CMakeFiles/CPOO-PROJECT-TEST.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/CPOO-PROJECT-TEST.dir/hello-test.cpp.o CMakeFiles/CPOO-PROJECT-TEST.dir/not_null_data-test.cpp.o -o CPOO-PROJECT-TEST -rdynamic -lgtest -lgtest_main -lpthread
CMakeFiles/CPOO-PROJECT-TEST.dir/not_null_data-test.cpp.o: In function `NOTNULL_flowtest_Test::TestBody()':
not_null_data-test.cpp:(.text+0x1e): undefined reference to `Processor::Processor()'
not_null_data-test.cpp:(.text+0x2d): undefined reference to `Processor::Detect()'
not_null_data-test.cpp:(.text+0xfb): undefined reference to `Processor::~Processor()'
not_null_data-test.cpp:(.text+0x145): undefined reference to `Processor::~Processor()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/CPOO-PROJECT-TEST.dir/build.make:126: CPOO-PROJECT-TEST] Error 1
make[3]: Leaving directory '/home/mateusz/Projects/EiTI/CPOO/build/test'
make[2]: *** [CMakeFiles/Makefile2:71: CMakeFiles/CPOO-PROJECT-TEST.dir/all] Error 2
make[2]: Leaving directory '/home/mateusz/Projects/EiTI/CPOO/build/test'
make[1]: *** [Makefile:87: all] Error 2
make[1]: Leaving directory '/home/mateusz/Projects/EiTI/CPOO/build/test'
make: *** [Makefile:13: build-test] Error 2

Related

Linking to TBB libraries with CMake

I have tbb downloaded and placed in my repository directory:
> tree deps/tbb/ -d
deps/tbb/
├── bin
├── cmake
│   └── templates
├── include
│   ├── serial
│   │   └── tbb
│   └── tbb
│   ├── compat
│   ├── internal
│   └── machine
└── lib
   ├── ia32
   │   └── gcc4.8
   └── intel64
   └── gcc4.8
In my CMakeLists.txt I have tried this:
include_directories("deps/tbb/include")
find_library(TBB_LIB
NAMES
tbbbind_debug
tbbbind
tbb_debug
tbbmalloc_debug
tbbmalloc_proxy_debug
tbbmalloc_proxy
tbbmalloc
tbb_preview_debug
tbb_preview
tbb
HINTS "${CMAKE_PREFIX_PATH}/deps/tbb/lib/intel64/gcc4.8"
)
add_executable(${PROJECT_NAME}
src/main.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC ${TBB_LIB})
But building with cmake, linker throws this error:
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: cannot find -lTBB_LIB-NOTFOUND
collect2: error: ld returned 1 exit status
I couldn't figure out what is missing. Thanks.
Update
This commit resolves the previous error:
- HINTS "${CMAKE_PREFIX_PATH}/deps/tbb/lib/intel64/gcc4.8"
+ HINTS "deps/tbb/lib/intel64/gcc4.8"
But, new errors are thrown:
undefined reference to `tbb::interface7::internal::task_arena_base::internal_current_slot()'
Update
Other than find_library, what CMake tools are available to link to TBB shared libraries?
I have tried some CMake tools, but I cannot figure out how to link to TBB *.so files correctly!
TBB has native CMake support. On my system with the Intel oneAPI installed, the config package is installed here:
/opt/intel/oneapi/tbb/latest/lib/cmake/tbb/TBBConfig.cmake
Therefore, I just need to add /opt/intel/oneapi/tbb/latest to my CMAKE_PREFIX_PATH. In my CMakeLists.txt, I wrote this:
cmake_minimum_required(VERSION 3.21)
project(test-tbb)
find_package(TBB REQUIRED)
add_library(main main.cpp)
target_link_libraries(main PRIVATE TBB::tbb)
target_compile_features(main PRIVATE cxx_std_11)
TBB provides the IMPORTED target TBB::tbb, which is what you should link to.
main.cpp is just the source from here: https://stackoverflow.com/a/36782794/2137996
Build like so:
$ cmake -G Ninja -S . -B build -DCMAKE_PREFIX_PATH=/opt/intel/oneapi/tbb/latest
$ cmake --build build --verbose
[1/2] /usr/bin/c++ -isystem /opt/intel/oneapi/tbb/2021.3.0/include -MD -MT CMakeFiles/main.dir/main.cpp.o -MF CMakeFiles/main.dir/main.cpp.o.d -o CMakeFiles/main.dir/main.cpp.o -c /home/alex/test2/main.cpp
[2/2] : && /usr/bin/cmake -E rm -f libmain.a && /usr/bin/ar qc libmain.a CMakeFiles/main.dir/main.cpp.o && /usr/bin/ranlib libmain.a && :
Inspired by #AlexReinking answer, here is the final implementation:
project(my-cpp-service VERSION 0.1.0)
# Equivalent to command-line option of `-DCMAKE_PREFIX_PATH=...`
list(APPEND CMAKE_MODULE_PATH "deps/tbb/cmake/")
find_package(TBB REQUIRED)
add_executable(${PROJECT_NAME}
src/main.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC
TBB::tbb
)
This post helped me solved the problem:
https://stackoverflow.com/a/41909627/3405291
The errors got resolved by:
include_directories("deps/tbb/include")
# https://stackoverflow.com/a/41909627/3405291
find_library(LIB_TBB NAMES tbb HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBbind NAMES tbbbind HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBmalloc_proxy NAMES tbbmalloc_proxy HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBBmalloc NAMES tbbmalloc HINTS "deps/tbb/lib/intel64/gcc4.8")
find_library(LIB_TBB_preview NAMES tbb_preview HINTS "deps/tbb/lib/intel64/gcc4.8")
add_executable(${PROJECT_NAME}
src/main.cpp
)
target_link_libraries(${PROJECT_NAME} PUBLIC
${LIB_TBB}
${LIB_TBBbind}
${LIB_TBBmalloc_proxy}
${LIB_TBBmalloc}
${LIB_TBB_preview}
)

mongodb c++ driver on ubuntu

I am trying to test mongodb c++ driver on Ubuntu 16.04.
The driver is installed in ${Devfolder}/sdk/mongodb/
and the test is in ${Devfolder}/testMongoDb/.
The code was compliled and tested using:
export PKG_CONFIG_PATH=${Devfolder}/sdk/mongodb/lib/pkgconfig
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${Devfolder}/sdk/mongodb/lib
c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
Then I want to use mongodb driver in my CMake project.
ugitho#ugitho:projects$ tree -L 3
.
├── sdk
│   └── mongodb
│   ├── bin
│   ├── include
│   ├── lib
│   └── share
├── testMongo
│   ├── a.out
│   ├── build
│   │   ├── CMakeCache.txt
│   │   ├── CMakeFiles
│   │   ├── cmake_install.cmake
│   │   ├── cmongodb
│   │   └── Makefile
│   ├── CMakeLists.txt
│   ├── test
│   └── test.cpp
But I got the following error:
ugitho#ugitho:build$ cmake ..
LIBMONGOCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/mongocxx/v_noabi;/home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBMONGOCXX_LIBRARIES = LIBMONGOCXX_LIBRARY_PATH-NOTFOUND;LIBBSONCXX_LIBRARY_PATH-NOTFOUND
LIBBSONCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = LIBBSONCXX_LIBRARY_PATH-NOTFOUND
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
LIBBSONCXX_LIBRARY_PATH
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
LIBMONGOCXX_LIBRARY_PATH
linked by target "cmongodb" in directory
/home/ugitho/NGUYENKHAC/projects/testMongoDb
-- Configuring incomplete, errors occurred!
See also "/home/ugitho/NGUYENKHAC/projects/testMongoDb/build/CMakeFiles/CMakeOutput.log".
edited*:
Here are CMakeLists.txt and test.cpp.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.2)
project(cmongodb)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH /home/ugitho/NGUYENKHAC/projects/sdk/mongodb)
#find_package(libmongocxx REQUIRED)
#find_package(libbsoncxx REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBMONGOCXX REQUIRED libmongocxx)
pkg_check_modules(LIBBSONCXX REQUIRED libbsoncxx)
message("LIBMONGOCXX_INCLUDE_DIRS = ${LIBMONGOCXX_INCLUDE_DIRS}")
message("LIBMONGOCXX_LIBRARIES = ${LIBMONGOCXX_LIBRARIES}")
message("LIBBSONCXX_INCLUDE_DIRS = ${LIBBSONCXX_INCLUDE_DIRS}")
message("LIBBSONCXX_LIBRARIES = ${LIBBSONCXX_LIBRARIES}")
set(COMMON_LIBRARIES ${LIBMONGOCXX_LIBRARIES} ${LIBBSONCXX_LIBRARIES})
set(SOURCE_FILES test.cpp)
add_executable(cmongodb ${SOURCE_FILES})
target_include_directories(cmongodb PUBLIC ${LIBMONGOCXX_INCLUDE_DIRS})
target_include_directories(cmongodb PUBLIC ${LIBBSONCXX_INCLUDE_DIRS})
target_link_libraries(cmongodb ${COMMON_LIBRARIES})
test.cpp:
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char **) {
std::cout << "1" << std::endl;
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{}};
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
document << "hello"
<< "world";
collection.insert_one(document.view());
auto cursor = collection.find({});
for (auto &&doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
return 0;
}
Do you have any idea about what I missed here?
Updates:
with the new CMakeLists.txt, there are no more cmake errors.
ugitho#ugitho:build$ cmake ..
-- Checking for module 'libmongocxx'
-- Found libmongocxx, version 3.4.0
-- Checking for module 'libbsoncxx'
-- Found libbsoncxx, version 3.4.0
CMAKE_PREFIX_PATH = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/shared
LIBMONGOCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/mongocxx/v_noabi;/home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBMONGOCXX_LIBRARIES = mongocxx;bsoncxx
LIBBSONCXX_INCLUDE_DIRS = /home/ugitho/NGUYENKHAC/projects/sdk/mongodb/include/bsoncxx/v_noabi
LIBBSONCXX_LIBRARIES = bsoncxx
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ugitho/NGUYENKHAC/projects/testMongoDb/build
[1]+ Done gedit /home/ugitho/NGUYENKHAC/projects/testMongoDb/build/CMakeFiles/CMakeOutput.log
The errors are now on make level:
ugitho#ugitho:build$ make
Scanning dependencies of target cmongodb
[ 50%] Building CXX object CMakeFiles/cmongodb.dir/test.cpp.o
[100%] Linking CXX executable cmongodb
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lmongocxx
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lbsoncxx
/opt/llvm/x86_64/6.0.0.g631a/bin/ld: error: unable to find library -lbsoncxx
collect2: error: ld returned 1 exit status
make[2]: *** [cmongodb] Error 1
make[1]: *** [CMakeFiles/cmongodb.dir/all] Error 2
make: *** [all] Error 2
UPDATE *** 27/12/2018
cmake_minimum_required(VERSION 3.2)
project(cmongodb)
set(CMAKE_CXX_STANDARD 14)
get_filename_component(PARENT_DIR ${CMAKE_CURRENT_SOURCE_DIR} DIRECTORY)
set(CMAKE_PREFIX_PATH ${PARENT_DIR}/sdk/mongodb)
message("CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}")
find_package(libmongocxx REQUIRED)
find_package(libbsoncxx REQUIRED)
set(MongoDb_INCLUDE_DIR ${PARENT_DIR}/sdk/mongodb/include)
include_directories(${MongoDb_INCLUDE_DIR}/mongocxx/v_noabi
${MongoDb_INCLUDE_DIR}/bsoncxx/v_noabi)
set(SOURCE_FILES test.cpp)
add_executable(cmongodb ${SOURCE_FILES})
target_link_libraries(cmongodb ${LIBMONGOCXX_LIBRARIES}
${LIBBSONCXX_LIBRARIES})
I managed to compile using this CMakeLists.txt.
new project config will be coming soon.
{GTest & Boost & MongoDb C++ Driver}
Thank you!!

Cmake: cannot open output file no such file or directory

I am learning to use cmake for a project using a shared library, but I keep getting this error:
Linking CXX executable test/test/robot_test
/usr/bin/ld: cannot open output file test/test/robot_test: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [test/test/robot_test] Error 1
make[1]: *** [CMakeFiles/test/robot_test.dir/all] Error 2
make: *** [all] Error 2
Here is my CMake file:
cmake_minimum_required(VERSION 2.8.12)
project("Particle Filter")
set(CMAKE_BUILD_TYPE Release)
include_directories(include)
set(LIB_SOURCES src/robot.cc src/sampler.cc src/general.cc)
set(LIB_HEADERS include/robot.h include/sampler.h include/general.h)
add_library(my_lib SHARED ${LIB_SOURCES} ${LIB_HEADERS})
install(TARGETS my_lib DESTINATION lib)
set(APP_SOURCES test/robot_test.cc test/sampler_test.cc)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
foreach(test ${APP_SOURCES})
#cut off .cc of src files using empty string
string(REPLACE ".cc" "" testname ${test})
add_executable(${testname} ${test})
target_link_libraries(${testname} my_lib)
endforeach(test ${APP_SOURCES})
add_definitions(
-std=c++11 # Or -std=c++0x
# Other flags
)
Here is my tree (excluding the build directory that contains a lot of this such as my makefile,.so and .a file):
├── CMakeLists.txt
├── driver.cc
├── include
│   ├── general.h
│   ├── robot.h
│   └── sampler.h
├── lib
├── notes
├── src
│   ├── general.cc
│   ├── robot.cc
│   └── sampler.cc
└── test
├── robot_test.cc
└── sampler_test.cc
Also, the .so or .a files are not getting saved to my lib folder after sudo make install, how do I fix this?
The first parameter for command add_executable is not a filename but a name of target. Target's name shouldn't contain special symbols like slash ("/").
You may control output directory of executables created by setting variable CMAKE_RUNTIME_OUTPUT_DIRECTORY:
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/test")
...
# Relative path to executable will be 'test/robot_test'.
add_executable(robot_test robot_test.cc)

CMake: undefined reference to

I use CMake 3.5.2. When trying to build my C++ code I get the following error:
[100%] Linking CXX executable SomeExecutable
CMakeFiles/SomeExecutable.dir/Common/src/FunctionOne.cpp.o: In function `FunctionOne::FunctionOne(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, double, unsigned int, bool)':
FunctionOne.cpp:(.text+0x490): undefined reference to `Helper::initLevel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
The program doesn't find Helper::initLevel() in myLib.so. How do I fix it?
CMakeLists.txt: (shortened for brevity)
cmake_minimum_required(VERSION 3.0)
project(ProjectName)
add_definitions(-std=c++11)
add_definitions(-Wall)
add_definitions(-O2)
link_directories(/usr/local/lib)
add_executable(SomeExecutable SomeExecutable.cpp ${FunctionOne} ${FunctionTwo})
target_link_libraries(SomeExecutable -pthread -lboost_thread ${Boost_LIBRARIES} myLib armadillo)
File structure (shortened for brevity):
├── bin
├── CMakeCache.txt
├── cmake_install.cmake
├── CMakeLists.txt
├── Common
│   ├── include
│   │   ├── dataContainer.h
│   │   ├── FunctionOne.h
│   │   ├── FunctionTwo.h
│   └── src
│   ├── FunctionOne.cpp
│   └── FunctionTwo.cpp
├── SomeExecutable.cpp
├── myLib
│   ├── example.cpp
│   ├── include
│   │   ├── Config.h
│   │   ├── Helper.h
│   ├── libmyLib.so
UPDATE:
myLib/include/Config.h:
Namespace Helper {
[...]
Level* initLevel(const std::string& jsonLvlPath, bool twiceSpatPts = false);
[..]
}
UPDATE 2:
kepj#laptop:~/ProjectName$ make VERBOSE=1
/usr/bin/cmake -E cmake_link_script CMakeFiles/SomeExecutable.dir/link.txt --verbose=1
/usr/bin/c++ CMakeFiles/SomeExecutable.dir/SomeExecutable.cpp.o CMakeFiles/SomeExecutable.dir/Common/src/FunctionOne.cpp.o CMakeFiles/SomeExecutable.dir/Common/src/FunctionTwo.cpp.o SomeExecutable -L/usr/local/lib -rdynamic -pthread -lboost_thread -lboost_system -lboost_filesystem -lmyLib -lsymbolicc++ -lmatio -larmadillo -Wl,-rpath,/usr/local/lib
CMakeFiles/SomeExecutable.dir/Common/src/FunctionTwo.cpp.o: In function `FunctionTwo::FunctionTwo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, double, double, unsigned int, bool)':
FunctionTwo.cpp:(.text+0x490): undefined reference to `Helper::initLevel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)'
collect2: error: ld returned 1 exit status
CMakeFiles/SomeExecutable.dir/build.make:174: recipe for target 'SomeExecutable' failed
make[2]: *** [SomeExecutable] Error 1
make[2]: Leaving directory '/home/kepj/ProjectName'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/SomeExecutable.dir/all' failed
make[1]: *** [CMakeFiles/SomeExecutable.dir/all] Error 2
make[1]: Leaving directory '/home/kepj/ProjectName'
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
So there is technically still not enough information about this issue to answer this question with certainty, but I am going to share how I would debug this isssue, only knowing what is in the question, explaining each step along the way.
Make sure you are linking to the correct myLib.so
This one is pretty explanatory, and would be my best guess as to the root of this problem.
Relevant compiler flags: -L/usr/local/lib -lmyLib
When trying to find the library you specified with the -l argument to your compile command, there is a series of steps that the linker takes:
1. It sees if the argument passed to -l was an absolute path (entire path to the library, ie starting with / on *nix computers or a drive letter on Windows) or just a name. If it is a absolute path, then it knows where it is. In this case, it is just a name, so continue on to step 2.
2. The next place to look is in the paths specified in -L arguments. In this case the only -L argument is to /usr/local/lib so it looks there. This may or may not be where it is found for this specific case. Look in your filesystem to see if the file /usr/local/lib/libmyLib.so exists, and if so make sure that is the version that is most recenpt.
3. Now it searches in configured linker paths. Use the same steps as in step 2 for each of these paths.
This is my guess because there is no reference to your myLib folder in your linker arguments, yet you claim it is in that folder, so I think it is using an out of date myLib.so.
Make sure the .so or .a file actually has the symbol that is undefined
On linux, you can list the symbols defined in a .so file. Make sure the symbol that is resolving to undefined is in there.
If they aren't there, here are the possible causes:
You aren't using all the C++ source files (.cpp) to create your library.
The definition and the declaration don't match:
You forgot to enter namespace Helper before defining the function
You misspelled the function name
The arguments/return type don't match
For member functions, you forgot to include MyClass:: before the function name.
ABI mismatch
(thanks to n.m in the comments for pointing this out)
Because there are multiple implementations (and versions) of the C++ standard library, it is possible that you have an ABI Mismatch.
This means that the standard library that myLib was compiled with is not compatable with the one you are trying to compile your executable with. For example, when you compile myLib with libstdc++ on linux, then go to mac and try to compile your executable, where the standard library is libc++ you will get errors. Why? The signature for anything, for example std::string is not the same. They are different classes entirely, and will cause a signature mismatch while linking.

CMake: Undefined reference to function

I have following directory structure:
├── build (empty dir)
├── CMakeLists.txt
├── easylogging++.h
├── help.h
├── operation.h
├── operation_list.h
├── operations
│   ├── CMakeLists.txt
│   ├── matchcount.cc
│   └── matchcount.h
└── ops_toolkit.cc
And I am new to CMake and trying to write CMakeLists.txt.
My matchcount.h has a signature whose implementation is in matchcount.cc (as typical C/C++ structure).
Following is my CMakeLists.txt in base directory
cmake_minimum_required(VERSION 2.6)
project(ops_toolkit)
add_subdirectory(operations)
add_executable(ops_toolkit ops_toolkit.cc)
set(CMAKE_CXX_FLAGS "-std=c++0x")
and following is the one in operations directory
include_directories(${ops_toolkit_SOURCE_DIR}/operations)
link_directories(${ops_toolkit_BINARY_DIR}/operations)
set(all_operations_HEADER operations/matchcount.h)
set(all_operations_SOURCES operations/matchcount.cc)
I am getting undefined reference for function signature called int matchcount(int, const char**) and make complains following
dev:~/work/ops_toolkit/build$ make
Scanning dependencies of target ops_toolkit
[100%] Building CXX object CMakeFiles/ops_toolkit.dir/ops_toolkit.cc.o
Linking CXX executable ops_toolkit
CMakeFiles/ops_toolkit.dir/ops_toolkit.cc.o: In function `__static_initialization_and_destruction_0(int, int)':
ops_toolkit.cc:(.text+0x249): undefined reference to `operations::matchcount(int, char const**)'
collect2: ld returned 1 exit status
make[2]: *** [ops_toolkit] Error 1
make[1]: *** [CMakeFiles/ops_toolkit.dir/all] Error 2
make: *** [all] Error 2
Can someone help me with this?
Thanks
The problem comes from the add_subdirectory and its associated CMakeLists.txt (the one in ./operations), which is incorrect, see this SO question for the implications of the add_subdirectory command.
I also advise you to put include_directories and link_directories in the main CMake file, for clarity.