0

CMake project, linking multiple sources, raising error: (at bottom is the full log)

[...%] Linking CXX executable ....
...: In function '...()':
....cpp:(.text+0x..): undefined reference to '...()'

I have 2 folders and 1 main-program:

  • maths (with sum.h/cpp and avg.h/cpp)
  • physics (with velocity.h/cpp and acceleration.h/cpp)
  • main.cpp

GOAL: To link main.cpp together with math and physics.

This is the project structure and content: (ignore the non-sense calculations)

app
├── CMakeLists.txt
                        cmake_minimum_required(VERSION 3.5)
                        set(CMAKE_CXX_STANDARD 14)
                        
                        project(app)
                        include_directories(src)
                        
                        add_library(maths STATIC src/maths/sum.cpp src/maths/avg.cpp)
                        add_library(physics STATIC src/physics/velocity.cpp src/physics/acceleration.cpp)
                        
                        add_executable(${PROJECT_NAME} src/main.cpp)
                        
                        target_link_libraries(${PROJECT_NAME} PUBLIC maths)
                        target_link_libraries(${PROJECT_NAME} PUBLIC physics)
                        
├── build
└── src
    ├── maths
    │   ├── avg.h
                        int calc_avg(int a, int b);
                        
    │   ├── avg.cpp
                        #include "avg.h"
                        int calc_avg(int a, int b) { return (a+b)/2; }
                        
    │   ├── sum.h
                        int calc_sum(int a, int b);
                        
    │   └── sum.cpp
                        #include "sum.h"
                        int calc_sum(int a, int b) { return a+b; }
                        
    ├── physics
    │   ├── velocity.h
                        int calc_velocity(int delta_1, int delta_2);
                        
    │   ├── velocity.cpp
                        #include "velocity.h"
                        #include "maths/sum.h"
                        #include "maths/avg.h"
                        int calc_velocity(int delta_1, int delta_2) {
                            return calc_avg(
                                calc_sum(delta_1, delta_1), 
                                calc_sum(delta_2, delta_2)
                            );
                        }
                        
    │   ├── acceleration.h
                        int calc_acceleration(int distance, int time);
                        
    │   └── acceleration.cpp
                        #include "acceleration.h"
                        #include "velocity.h"
                        int calc_acceleration(int distance, int time) {
                            return calc_velocity(distance*distance, time*time);
                        }
                        
    └── main.cpp
                        #include <iostream>
                        using namespace std;
                        
                        #include "physics/velocity.h"
                        #include "physics/acceleration.h"
                         
                        int main() {
                                cout << calc_velocity(110, 220) << endl;
                                cout << calc_acceleration(1100, 2200) << endl;
                                return 0;
                        }

This is the output after running cmake .. && make from app/build/

-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /app/build
Scanning dependencies of target maths
[ 12%] Building CXX object CMakeFiles/maths.dir/src/maths/sum.cpp.o
[ 25%] Building CXX object CMakeFiles/maths.dir/src/maths/avg.cpp.o
[ 37%] Linking CXX static library libmaths.a
[ 37%] Built target maths
Scanning dependencies of target physics
[ 50%] Building CXX object CMakeFiles/physics.dir/src/physics/velocity.cpp.o
[ 62%] Building CXX object CMakeFiles/physics.dir/src/physics/acceleration.cpp.o
[ 75%] Linking CXX static library libphysics.a
[ 75%] Built target physics
Scanning dependencies of target mycmakeapp
[ 87%] Building CXX object CMakeFiles/mycmakeapp.dir/src/main.cpp.o
[100%] Linking CXX executable mycmakeapp
libphysics.a(velocity.cpp.o): In function `calc_velocity(int, int)':
velocity.cpp:(.text+0x1a): undefined reference to `calc_sum(int, int)'
velocity.cpp:(.text+0x2b): undefined reference to `calc_sum(int, int)'
velocity.cpp:(.text+0x34): undefined reference to `calc_avg(int, int)'
collect2: error: ld returned 1 exit status
CMakeFiles/mycmakeapp.dir/build.make:96: recipe for target 'mycmakeapp' failed
make[2]: *** [mycmakeapp] Error 1
CMakeFiles/Makefile2:68: recipe for target 'CMakeFiles/mycmakeapp.dir/all' failed
make[1]: *** [CMakeFiles/mycmakeapp.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Johnny
  • 336
  • 2
  • 10
  • 1
    Swap the order of `target_link_libraries`. – Yksisarvinen Feb 12 '21 at 10:58
  • 2
    Why not `target_link_libraries(physics PUBLIC maths)` ? It depends on it, no? – KamilCuk Feb 12 '21 at 10:59
  • Did you generate the file listing by hand? Or with a tool? It looks great. Is `tree` capable of this? – KamilCuk Feb 12 '21 at 11:05
  • [Generic question](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) about "undefined references" has the answer specifically for your case: https://stackoverflow.com/a/24675715/3440745. – Tsyvarev Feb 12 '21 at 11:08

0 Answers0