0

Here i am creating a project . i am using cmake . i am not able to link cmake created shared library with my main application ??? My CMakeLists.txt file are as follows :

//tree -stucture of my project

├── CMakeLists.txt
├── include
│   ├── Account.h
│   ├── bankingSystem.h
│   ├── bankStruct.h
│   └── stdheader.h
├── main.cpp
└── src
    ├── Account.cpp
    ├── bankingSystem.cpp
    └── main.cpp

//CMakeLists.txt
cmake_minimum_required(VERSION 3.17.1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -std=c++11 -DPC_BUILD")

include_directories(
        ./include
        )
link_directories(
        /usr/local/lib)
#create a shared library to be linked with test application
add_library(
        BANK SHARED
        src/bankingSystem.cpp
        src/Account.cpp
        )
add_executable(
        main main.cpp
        )
message("BANK")

But i am getting Undefined Reference :

[ 60%] Built target BANK
[ 80%] Linking CXX executable main
CMakeFiles/main.dir/main.cpp.o: In function `main':
/home/pankaj/BANK/main.cpp:8: undefined reference to `BankingSystem::BankingSystem()'
/home/pankaj/BANK/main.cpp:19: undefined reference to `BankingSystem::Create_new_Account()'
...
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:103: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:124: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:103: recipe for target 'all' failed
make: *** [all] Error 2
Botje
  • 26,269
  • 3
  • 31
  • 41
  • 2
    [target_link_libraries](https://cmake.org/cmake/help/latest/command/target_link_libraries.html)? – Alan Birtles Jul 31 '20 at 06:51
  • As @AlanBirtles says, you need to actually tell CMake that you want to link `main` with `BANK`. – Botje Jul 31 '20 at 08:55
  • 1
    You need to *link* your library to the executable, as the similar responses [here](https://stackoverflow.com/questions/43964691/cmake-undefined-reference) and [here](https://stackoverflow.com/q/38530491/3987854) explain. – Kevin Jul 31 '20 at 14:16

0 Answers0