Error during compilation with multiple source files

147 views
Skip to first unread message

sabyasachi chatterjee

unread,
Aug 30, 2023, 2:22:46 AM8/30/23
to deal.II User Group
Hello,

I am trying to compile a project 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:
---
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.

<${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:
---
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:

<${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 have attached all the relevant files with only the relevant lines of code. I would appreciate any help.

Thank you.
ibvp.cc
main.cc
CMakeLists_src.txt
CMakeLists_main.txt
ibvp.h

Wolfgang Bangerth

unread,
Aug 30, 2023, 12:29:05 PM8/30/23
to dea...@googlegroups.com
On 8/30/23 00:22, sabyasachi chatterjee wrote:
>
> /home/sabyasachi/Documents/current_research/irradiated_materials/irradiated_tension_engg_400C/main.cc:96:
> error: undefined reference to 'ibvp<3>::ibvp()'

You will likely have to add 'explicit instantiations' to the bottom of
the .cc file. Like here:
https://stackoverflow.com/questions/2351148/explicit-template-instantiation-when-is-it-used

Best
W.

--
------------------------------------------------------------------------
Wolfgang Bangerth email: bang...@colostate.edu
www: http://www.math.colostate.edu/~bangerth/

sabyasachi chatterjee

unread,
Aug 31, 2023, 3:51:14 AM8/31/23
to deal.II User Group
I added at the end of ibvp.h:
typedef ibvp<3> ibvp_obj;

I added at the end of ibvp.cc:
template class ibvp<3>;

Then in main.cc, I create the object as
ibvp_obj irradiation;

It is still returning exactly the same error of undefined reference.

/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:128: error: undefined reference to 'ibvp<3>::~ibvp()'
/home/sabyasachi/Documents/current_research/irradiated_materials/irradiated_tension_engg_400C/main.cc:128: 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

I also tried template class ibvp_obj<3> at the end of .cc but still the same error.

Wolfgang Bangerth

unread,
Aug 31, 2023, 10:56:29 AM8/31/23
to dea...@googlegroups.com

Sabyasachi,
I know what the error means, but it's impossible to tell without seeing
the code. Are you sure you are linking the executable you create from
main.cc against the shared library you create?

Best
W.

On 8/31/23 01:51, sabyasachi chatterjee wrote:
> *** Caution: EXTERNAL Sender ***
> <http://www.math.colostate.edu/~bangerth/>
>
> --
> The deal.II project is located at http://www.dealii.org/
> <http://www.dealii.org/>
> For mailing list/forum options, see
> https://groups.google.com/d/forum/dealii?hl=en
> <https://groups.google.com/d/forum/dealii?hl=en>
> ---
> You received this message because you are subscribed to the Google
> Groups "deal.II User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to dealii+un...@googlegroups.com
> <mailto:dealii+un...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/dealii/2bd60445-4b14-424e-bb05-bf07cc73d405n%40googlegroups.com
> <https://groups.google.com/d/msgid/dealii/2bd60445-4b14-424e-bb05-bf07cc73d405n%40googlegroups.com?utm_medium=email&utm_source=footer>.

sabyasachi chatterjee

unread,
Aug 31, 2023, 2:09:21 PM8/31/23
to deal.II User Group
Prof. Bangerth,

I think yes, but I can't be sure. I have created a minimum working example (attached) for your reference. Only main.cc , src/ibvp.h and src/ibvp.h have a few lines of code. Rest all files are cleaned.

One thing I notice is that ibvp.cc is being compiled in the middle of the compilation but it probably should be done after all the member functions are compiled.

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

I am not sure if the error is related to this or not.
mwe.zip

Wolfgang Bangerth

unread,
Aug 31, 2023, 3:23:39 PM8/31/23
to dea...@googlegroups.com
On 8/31/23 12:09, sabyasachi chatterjee wrote:
>
> I think yes, but I can't be sure. I have created a minimum working
> example (attached) for your reference. Only main.cc , src/ibvp.h and
> src/ibvp.h have a few lines of code. Rest all files are cleaned.

I do not see where you have added the explicit instantiation. Adding
this line to the end of ibvp.cc fixed the problem for me:

template class ibvp<3>;

sabyasachi chatterjee

unread,
Sep 2, 2023, 11:52:12 AM9/2/23
to deal.II User Group
Yes, that fixed the problem. Thanks a lot.
Reply all
Reply to author
Forward
0 new messages