Introducing global object into a library

Apr 13, 2016 at 4:09pm
Hello.

If I have header file basicKinematicCloud.H containing

#ifndef basicKinematicCloud_H
#define basicKinematicCloud_H

#include "Cloud.H"
#include "KinematicCloud.H"
#include "basicKinematicParcel.H"

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

namespace Foam
{
typedef KinematicCloud<Cloud<basicKinematicParcel> > basicKinematicCloud;
}

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

#endif


and then in main file (before int main...) there is

basicKinematicCloud kinematicCloud
(
kinematicCloudName,
rhoInf,
U,
CurlU,
mu,
g
);


How can I introduce above mentioned kinematicCloud in other library ? I have
tried with putting

#include "basicKinematicCloud.H"

extern basicKinematicCloud kinematicCloud;

in header file of library in which I need to introduce this kinematicCloud, but then there is an error

error: ‘basicKinematicCloud’ does not name a type

Listed code is part of OpenFOAM, but problem is of programming nature. I hope that somebody here can help me.


Regards,
Darko







Apr 13, 2016 at 4:52pm
> error: ‘basicKinematicCloud’ does not name a type
I'm going to guess that you forgot the namespace.
extern Foam::basicKinematicCloud kinematicCloud;

If that doesn't work, try to provide a minimal example (the library is quite big, just your files may be enough)


Remember to define it in one cpp and link it to your project.
Apr 13, 2016 at 6:39pm
It solved partially problem. Thank you very much for that!

Now I have error

 error: expected primary-expression before ‘.’ token
                                       1.0/rho * (kinematicCloud.Spkepsilon());


In library (mykEpsilon.C) I wrote:
(this is not optimized, I just want it to work for now)

1
2
DimensionedField<scalar, volMesh> epsilonParticleSource = 
                                      1.0/rho * (kinematicCloud.Spkepsilon());



I defined method Spkepsilon() (in KinematicCloudI.H) with

1
2
3
4
5
6
template<class CloudType>
inline Foam::DimensionedField<Foam::scalar, Foam::volMesh>  
Foam::KinematicCloud<CloudType>::Spkepsilon() const
{
   ...
}


I have started relatively recently to use C++.
Apr 13, 2016 at 7:41pm
Don't see anything obviously wrong, but I'm missing a lot of context.
Please consider this http://www.cplusplus.com/forum/articles/40071/#msg216313

May also try to simplify the code. By instance, remove epsilonParticleSource and rho
kinematicCloud.Spkepsilon();


If you need several files to reproduce the problem, upload your code to github or similar.
Apr 14, 2016 at 7:00am
Ok. I understand you, I haven't provided enough information.

The most important files with most important parts are on this link

https://www.dropbox.com/s/jci9us353yg5jo5/GlobalObjectProblem.zip?dl=0

If you need whole files or any other file, just tell me. Thanks for your time and patience.

I tried to simplify the code, but the error is still the same.

 mykEpsilon.C:244:77: error: expected primary-expression before ‘.’ token
     DimensionedField<scalar, volMesh> epsilonParticleSource = kinematicCloud.Spkepsilon();



Apr 14, 2016 at 9:55am
I'm a little confused.
In KinematicCloud.H you have
1
2
3
4
5
template<class CloudType>
class KinematicCloud
:
    public CloudType,
    public kinematicCloud
so `kinematicCloud' is a type

But in mykEpsilon.H, it is an object extern Foam::basicKinematicCloud kinematicCloud;


I guess that your problem is that you have name collision.
You have the type Foam::kinematicCloud and the object ::kinematicCloud
Because you are defining functions inside the `Foam' namespace, the compiler thinks that you are refering to the type instead of the object.

As a solution, use ::kinematicCloud when referring to the object or change its name so there is no confusion.
Apr 14, 2016 at 12:50pm
You were right! I have changed the name of the object into kinematicCloudObject and I successfully compiled library.

However, when I compile the main program, I have error

libincompressibleTurbulenceModels.so: undefined reference to `kinematicCloudObject'



Additional files are here
https://www.dropbox.com/s/4o3qgm40z79jl42/TurbulenceModels.zip?dl=0

This folder structure is in OpenFOAM. I didn't send everything, I deleted unnecessary files.

I tried to put declarations

extern Foam::basicKinematicCloud kinematicCloud;

in incompressibleTurbulenceModel.H and in incompressibleTurbulenceModel.H but error is still the same.



Apr 14, 2016 at 1:51pm
http://www.cplusplus.com/forum/general/113904/#msg622050
¿did you remove the definition?
`extern' only says that there is a global variable, but you need to define it in some source as a global variable

and then in main file (before int main...) there is

1
2
3
4
5
6
7
8
9
basicKinematicCloud kinematicCloud
(
   kinematicCloudName, 
   rhoInf,
   U,
   CurlU,
   mu,
   g
);
I saw that definition in `createFields.H'

In `simpleKinematic2wFoam.C you've got #include "createFields.H" inside the `main()' function. So you are creating a local variable.

The definition should be outside main() or any function in order to be global.
Last edited on Apr 14, 2016 at 1:52pm
Apr 14, 2016 at 3:46pm
Yes, I completely forgot that I have #include "createFields.H" inside the main function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main(int argc, char *argv[])
{
  
    
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"
    #include "readGravitationalAcceleration.H"


    simpleControl simple(mesh);

    #include "createFields.H"
    #include "createMRF.H"
    #include "createFvOptions.H"
    #include "initContinuityErrs.H" 


I can't pull out these files in front of int main. I believe it has to be here. This part of file was my starting point when I started to write my additional code. So I think I can't use the concept of introducing global object into my library.
I have to use the other way. I am not sure, but I think that I have to introduce my object into library as a parameter of a function. I have to look more into the code for that.

I am very grateful to you for every advice that you gave me.
Topic archived. No new replies allowed.