Class function locations - linker error

30 views
Skip to first unread message
Assigned to andrew....@gmail.com by lik...@wp.pl

kulikova.adriana

unread,
Nov 8, 2021, 7:51:23 AM11/8/21
to MoFEM Q&A
Hello,

I am in the process of tidying up my module and create a class I want to inherit (plus headers of all of the functions in it) in /src/DiffusionClassic.hpp. The full definitions of the introduced functions are located in /DiffusionClassic.cpp file.

Another file, /src/DiffusionDD.hpp, creates a class which wants to inherit the previously created file but causes linker error whenever I want to use functions whose parts are in the first /DiffusionClassic.cpp. Once the functions required are moved to the /src/DiffusionClassic.hpp everything works.

Is there something missing in my CMakeLists.txt file (attached)?

The linker error is shown bellow:
Undefined symbols for architecture x86_64:
  "DiffusionClassic::DiffusionClassic(MoFEM::DeprecatedCoreInterface&)", referenced from:
      DiffusionDD::DiffusionDD(MoFEM::DeprecatedCoreInterface&) in diffusion_DD.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Thank you,
Adriana
CMakeLists.txt

Lukasz Kaczmraczyk

unread,
Nov 9, 2021, 5:39:12 AM11/9/21
to MoFEM Q&A

Hello Adriana,

I apologize for late response. The simple fix would be to add source to cmake function dd_build_and_install, i.e.

### build and install
# function definition for building
function(dd_build_and_install target source)
  bfe_add_executable(${target} ${source} PATH/DiffusionClassic.cpp)
  target_link_libraries(${target}
    users_modules
    mofem_finite_elements
    mofem_interfaces
    mofem_multi_indices
    mofem_petsc
    mofem_approx
    mofem_third_party
    ${MoFEM_PROJECT_LIBS})
  install(TARGETS ${target} DESTINATION ${MODULE_NAME})
endfunction(dd_build_and_install)

Only question what is path, typically, for cpp lib files it is,
src/impl/DiffusionClassic.cpp

Another option is to make library, but I think that above solution is sufficient at this point.

L.

Message has been deleted

Karol Lewandowski

unread,
Nov 9, 2021, 1:54:16 PM11/9/21
to MoFEM Q&A

But creating a library is really not necessary in this case. One only has to make sure now that the #include of header files are in the correct order.
On Tuesday, 9 November 2021 at 18:50:43 UTC andrew....@gmail.com wrote:
Hi all,

Lukasz's answer gives only a part of the solution to your issue. 

In general, you should not have the main function in the cpp file which has definitions of member functions of some class. The idea of incapsulation is that the definition of the class is separated from a function that creates an object of this class and works with it. 
You can have a look at a diagram at the beginning of this article: https://makefiletutorial.com 
What follows after in that article is the introduction to makefiles, which could be useful as well. 

One way of encapsulating your class (h and cpp files) is creating a library, as Lukasz mentioned above. In fact, I think this is the solution that would be most useful for your code. You can look into this tutorial on Cmake, in particular, Step 2 (Adding a library) as it covers a similar case:  https://cmake.org/cmake/help/latest/guide/tutorial

Regards,
Andrei

Andrei G. Shvarts

unread,
Nov 9, 2021, 2:03:19 PM11/9/21
to MoFEM Q&A
Hi all, 

Lukasz's answer gives only a part of the solution to your issue. 
In general, you should not have the main function in cpp file which has definitions of member functions of some class. The idea of encapsulation is that the definition of lthe class is separated from a function that creates an object of this class and works with it. 
You can have a look at a diagram at the beginning of this article: https://makefiletutorial.com

What follows after in that article is the introduction to makefiles, which could be useful as well. 
One way of encapsulating your class (h and cpp files) is creating a library, as Lukasz mentioned above. You can look into this tutorial on CMake, in particular, Step 2 (Adding a library) as it covers a similar case:  https://cmake.org/cmake/help/latest/guide/tutorial

Regards,
Andrei
Reply all
Reply to author
Forward
0 new messages