CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Undefined reference to custom class

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 28, 2018, 23:37
Default Undefined reference to custom class
  #1
Senior Member
 
Ruiyan Chen
Join Date: Jul 2016
Location: Hangzhou, China
Posts: 162
Rep Power: 9
cryabroad is on a distinguished road
Hi Guys,

I developed a class of my own named testPdfTable. I had both the .H file and .C file written, then I used wmake libso to compile it. The compilation runs smooth without any errors, and a .so file named libtestPdfTable.so is generated without any problem.

After this I wrote a simple program (Test-pdf.C) to test the above class. The Make/files file looks like the following

Code:
Test-pdf.C

EXE = $(FOAM_USER_APPBIN)/Test-pdf
and the Make/options file looks like the following

Code:
EXE_INC = \
    -I$(realpath ../../myThermoModels/basic/testPdfTable/lnInclude) \

LIB_LIBS = \
    -L$(FOAM_USER_LIBBIN) \
    -ltestPdfTable
The EXE_INC input points to the path where the class is defined, the LIB_LIBS input points to the .so file for the class. The LIB_LIBS input seems to be not necessary because the error message (below) I got is the same if I omitted this input.

When I run wmake the compiler gives me the following error message

Code:
Making dependency list for source file Test-pdf.C
g++ -std=c++0x -m64 -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -O3  -DNoRepository -ftemplate-depth-100 -I/home/ruiyan/OpenFOAM/ruiyan-4.x/myThermoModels/basic/testPdfTable/lnInclude  -IlnInclude -I. -I/home/ruiyan/OpenFOAM/OpenFOAM-4.x/src/OpenFOAM/lnInclude -I/home/ruiyan/OpenFOAM/OpenFOAM-4.x/src/OSspecific/POSIX/lnInclude   -fPIC -c Test-pdf.C -o Make/linux64GccDPInt32Opt/Test-pdf.o
g++ -std=c++0x -m64 -Dlinux64 -DWM_ARCH_OPTION=64 -DWM_DP -DWM_LABEL_SIZE=32 -Wall -Wextra -Wold-style-cast -Wnon-virtual-dtor -Wno-unused-parameter -Wno-invalid-offsetof -O3  -DNoRepository -ftemplate-depth-100 -I/home/ruiyan/OpenFOAM/ruiyan-4.x/myThermoModels/basic/testPdfTable/lnInclude  -IlnInclude -I. -I/home/ruiyan/OpenFOAM/OpenFOAM-4.x/src/OpenFOAM/lnInclude -I/home/ruiyan/OpenFOAM/OpenFOAM-4.x/src/OSspecific/POSIX/lnInclude   -fPIC -Xlinker --add-needed -Xlinker --no-as-needed Make/linux64GccDPInt32Opt/Test-pdf.o -L/home/ruiyan/OpenFOAM/OpenFOAM-4.x/platforms/linux64GccDPInt32Opt/lib \
     -lOpenFOAM -ldl  \
     -lm -o /home/ruiyan/OpenFOAM/ruiyan-4.x/platforms/linux64GccDPInt32Opt/bin/Test-pdf
Make/linux64GccDPInt32Opt/Test-pdf.o: In function `main':
Test-pdf.C:(.text.startup+0x5a): undefined reference to `Foam::testPdfTable::testPdfTable()'
Test-pdf.C:(.text.startup+0x62): undefined reference to `Foam::testPdfTable::ReadPDF()'
Test-pdf.C:(.text.startup+0xf3): undefined reference to `Foam::testPdfTable::interpolate(int, double) const'
Test-pdf.C:(.text.startup+0x1cb): undefined reference to `Foam::testPdfTable::interpolate(int, double) const'
Test-pdf.C:(.text.startup+0x2b4): undefined reference to `Foam::testPdfTable::~testPdfTable()'
Test-pdf.C:(.text.startup+0x304): undefined reference to `Foam::testPdfTable::~testPdfTable()'
collect2: error: ld returned 1 exit status
/home/ruiyan/OpenFOAM/OpenFOAM-4.x/wmake/makefiles/general:132: recipe for target '/home/ruiyan/OpenFOAM/ruiyan-4.x/platforms/linux64GccDPInt32Opt/bin/Test-pdf' failed
make: *** [/home/ruiyan/OpenFOAM/ruiyan-4.x/platforms/linux64GccDPInt32Opt/bin/Test-pdf] Error 1
As you can tell there seems to be a linker problem. The Test-pdf.C cannot identify all the member functions of class testPdfTable. Basically, Test-pdf.C cannot identify this class, even with all the headers properly defined and located (if not the compiler will complain that there is no testPdfTable.H or similar things).

EDIT: I compiled the class and the test function using gcc on Linux system. It worked pretty well. So my problem is more likely to be a linking issue in OF.

What could be the problem and how can this be fixed?

Thank you guys in advance.

BTW I'm running OF-4.x on a virtual machine using Ubuntu.

Last edited by cryabroad; July 2, 2018 at 02:17.
cryabroad is offline   Reply With Quote

Old   June 29, 2018, 02:22
Default
  #2
Senior Member
 
zhangyan's Avatar
 
Yan Zhang
Join Date: May 2014
Posts: 120
Rep Power: 11
zhangyan is on a distinguished road
It seems that you just declare these functions in .H but not defined them in .C
__________________
https://openfoam.top
zhangyan is offline   Reply With Quote

Old   July 1, 2018, 22:43
Default
  #3
Senior Member
 
Ruiyan Chen
Join Date: Jul 2016
Location: Hangzhou, China
Posts: 162
Rep Power: 9
cryabroad is on a distinguished road
Hi Zhangyan,

Thank you for the suggestions. I've double checked to make sure that the functions are both declared and defined. I actually compiled the class (.H and .C) and the main function Test-pdf.C using the build-in gcc compiler for Linux system. The program runs pretty well.

I will add the above info to my original post so people can have a better idea of what I've done.

Thank you again.
cryabroad is offline   Reply With Quote

Old   July 2, 2018, 21:19
Default
  #4
Senior Member
 
zhangyan's Avatar
 
Yan Zhang
Join Date: May 2014
Posts: 120
Rep Power: 11
zhangyan is on a distinguished road
Have you solved this issue? I met the same problem...
__________________
https://openfoam.top
zhangyan is offline   Reply With Quote

Old   July 3, 2018, 00:56
Default
  #5
Senior Member
 
zhangyan's Avatar
 
Yan Zhang
Join Date: May 2014
Posts: 120
Rep Power: 11
zhangyan is on a distinguished road
Maybe I found a solution for your problem:
In the solver's options file, it should have a EXE_LIBS, instead of LIB_LIBS.
__________________
https://openfoam.top
zhangyan is offline   Reply With Quote

Old   July 3, 2018, 02:21
Default
  #6
Senior Member
 
Ruiyan Chen
Join Date: Jul 2016
Location: Hangzhou, China
Posts: 162
Rep Power: 9
cryabroad is on a distinguished road
Dear Zhangyan,

It really works! I've never noticed that this part of code is that important. After all it all makes sense now: when we compile to create libraries, we use EXE_INC for including other header files, and LIB_LIBS for including other libraries; when we compile to get executable files, we change LIB_LIBS to EXE_LIBS.

This is clearly shown in OF source code, the Make file for libraries are paired with EXE_INC and LIB_LIBS, and the Make file for solvers are paired with EXE_INC and EXE_LIBS. I should've noticed that earlier, feel so stupid right now. I wonder if the EXE_INC is also changeable.

Thank you for your help, really appreciate it!
cryabroad is offline   Reply With Quote

Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
-Xlinker --add-needed -Xlinker --no-as-needed NH1994 OpenFOAM Programming & Development 7 December 31, 2014 10:24
openFoam-1.7.x installation Roman1 OpenFOAM Installation 4 October 2, 2013 08:02
Reynolds Stress Custom BC maninthemail OpenFOAM 3 November 14, 2011 13:58
OpenFoam 1.6-ext - RPM build errors preibie OpenFOAM 12 September 8, 2011 03:12
OF Successfully built? Pytthon OpenFOAM 5 February 20, 2010 06:44


All times are GMT -4. The time now is 21:00.