# Undefined reference during project compilation

**URL:** https://discourse.cmake.org/t/undefined-reference-during-project-compilation/8850
**Category:** Usage
**Created:** [August 31, 2023, 8:04am UTC](https://discourse.cmake.org/t/undefined-reference-during-project-compilation/8850 "2023-08-31T08:04:12Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sabyasachi\_chatterje](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sabyasachi_chatterje/32/3765_2.png) [@sabyasachi\_chatterje](https://discourse.cmake.org/u/sabyasachi_chatterje)
#### Post date: [August 31, 2023, 8:04am UTC](https://discourse.cmake.org/t/undefined-reference-during-project-compilation/8850/1 "2023-08-31T08:04:12Z")

</div>

Hello,

I am trying to compile a project (based on the FEM library deal ii) with multiple source and header files. I am new to CMake. In the main directory, there is main.cc file. All other files including header and source files are stored in a subdirectory src. The class ibvp is similar to TopLevel class in step 18. It is defined in src/ibvp.h and src/ibvp.cc . In the main file, I have added #include “src/ibvp.h”. There are 2 CMakeLists.txt, one in the main folder and another in the src folder. The one in the src folder is used to create a library mylib with all the source files. The modified lines in this cmake file are:

```cmake
SET(TARGET "mylib")

PROJECT(${TARGET})

SET(CMAKE_BUILD_TYPE Debug)

file(GLOB mylib_files *.cc)

add_library(mylib ${mylib_files})

DEAL_II_SETUP_TARGET(${TARGET})

```

Both cmake and make are running without error.

```plaintext
<${main_directory}/src>$ make -j8

[11%] Building CXX object CMakeFiles/mylib.dir/assemble.cc.o
[22%] Building CXX object CMakeFiles/mylib.dir/boundarycondition.cc.o
[33%] Building CXX object CMakeFiles/mylib.dir/ibvp.cc.o
[44%] Building CXX object CMakeFiles/mylib.dir/output.cc.o
[55%] Building CXX object CMakeFiles/mylib.dir/setup_system.cc.o
[66%] Building CXX object CMakeFiles/mylib.dir/solve.cc.o
[77%] Building CXX object CMakeFiles/mylib.dir/utils.cc.o
[88%] Building CXX object CMakeFiles/mylib.dir/gausspoint.cc.o
[100%] Linking CXX static library libmylib.a
[100%] Built target mylib

```

After this, I try to compile the main.cc code by linking it with the library mylib in the src folder. The modified lines in this cmake file are:

```cmake
SET(TARGET "main")

PROJECT(${TARGET})

SET(CMAKE_BUILD_TYPE Debug)

INCLUDE_DIRECTORIES(src)
add_subdirectory(src)

ADD_EXECUTABLE(${TARGET} ${TARGET}.cc)

DEAL_II_SETUP_TARGET(${TARGET})

target_link_libraries(${TARGET} mylib)

```

CMake is not giving any error. But when I run make, I get the following error:

```plaintext
<${main_directory}>$ make -j8
Consolidate compiler generated dependencies of target mylib
[9%] Building CXX object src/CMakeFiles/mylib.dir/assemble.cc.o
[18%] Building CXX object src/CMakeFiles/mylib.dir/boundarycondition.cc.o
[27%] Building CXX object src/CMakeFiles/mylib.dir/gausspoint.cc.o
[36%] Building CXX object src/CMakeFiles/mylib.dir/ibvp.cc.o
[45%] Building CXX object src/CMakeFiles/mylib.dir/output.cc.o
[54%] Building CXX object src/CMakeFiles/mylib.dir/setup_system.cc.o
[63%] Building CXX object src/CMakeFiles/mylib.dir/solve.cc.o
[72%] Building CXX object src/CMakeFiles/mylib.dir/utils.cc.o
[81%] Linking CXX static library libmylib.a
[81%] Built target mylib
[90%] Building CXX object CMakeFiles/main.dir/main.cc.o
[100%] Linking CXX executable main
/home/sabyasachi/Documents/current_research/irradiated_materials/irradiated_tension_engg_400C/main.cc:96: error: undefined reference to 'ibvp<3>::ibvp()'
/home/sabyasachi/Documents/current_research/irradiated_materials/irradiated_tension_engg_400C/main.cc:98: error: undefined reference to 'ibvp<3>::run()'
/home/sabyasachi/Documents/current_research/irradiated_materials/irradiated_tension_engg_400C/main.cc:99: error: undefined reference to 'ibvp<3>::~ibvp()'
/home/sabyasachi/Documents/current_research/irradiated_materials/irradiated_tension_engg_400C/main.cc:99: error: undefined reference to 'ibvp<3>::~ibvp()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:132: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

```

So there are undefined references to the ibvp class constructor, destructor and run function. Please suggest how to resolve this. I would appreciate any help.

Thank you.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [September 1, 2023, 11:25am UTC](https://discourse.cmake.org/t/undefined-reference-during-project-compilation/8850/2 "2023-09-01T11:25:51Z")

</div>

I edited your post to improve formatting.

It looks like `mylib`’s `ibvp.cc` is likely missing some definitions. Does it contain implementations for these functions? Can you share `ibvp`’s header and source here?
