Problem in calling a .hh file.

For this question, i have basically 4 main files, let's call it:

Constant.hh

Constant.cc (where a define the function)

Field.hh

Field.cc (where i call the function)

So basically, i have defined a function at the file "Constant.cc" and i am calling it to be used at Field.cc. But when i try to run it on terminal (using make), it returns this:

1
2
3
    /usr/bin/ld: CMakeFiles/eRosita.dir/application/src/Field.cc.o: in function "FF::GetFieldValue(double const*, double*) const":
    Field.cc:(.text+0xcc): undefined reference for "Constant(int, double)"
    collect2: error: ld returned 1 exit status


And i am not sure why.

The content inside thes four files are:

Constant.hh
-----------
1
2
3
4
5
6
    #ifndef Constant_h
    #define Constant_h 1
    
    double Constant(int f, double p);
    
    #endif 


Constant.cc

-----------
1
2
3
4
5
6
7
8
    #include <iostream>
    
    double Constant(int f, double p) {
    if (f == 30){
    return 3;}
    else{
    return 0;}
    }


Field.hh
--------
1
2
3
4
5
6
7
8
9
10
11
12
    #ifndef Field_h
    #define Field_h 1

    class FF : public G4ElectroMagneticField {
    
        public:
            ~FF();
            virtual void  GetFieldValue(const G4double Point[4], G4double *Bfield ) const override;
            virtual G4bool DoesFieldChangeEnergy() const override;
    
    };
    #endif 


Field.cc
--------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    #include "Constant.hh"
    
    FF::~FF(){}
    
    G4bool FF::DoesFieldChangeEnergy() const{
        return true;
    }
    
    void FF::GetFieldValue(const G4double Point[4], G4double field[6]) const
         
        double r = sqrt(x*x+y*y);
    
        double p = 2*pi*r/(400*0.025);
        int f = -20;
        
        double E = Constant(f,p);
      }


(OBS: the unecessary part of the code i have just deleted, so that the question get more summarized)
Last edited on
My guess is that your build scripts are not compiling and/or linking Constant.o
@Ganado
I am not sure, but i think it is. At the Makefile, we have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
application/src/Constant.o: application/src/Constant.cc.o

.PHONY : application/src/Constant.o

# target to build an object file
application/src/Constant.cc.o:
	$(MAKE) -f CMakeFiles/eRosita.dir/build.make CMakeFiles/eRosita.dir/application/src/Constant.cc.o
.PHONY : application/src/Constant.cc.o

application/src/Constant.i: application/src/Constant.cc.i

.PHONY : application/src/Constant.i

# target to preprocess a source file
application/src/Constant.cc.i:
	$(MAKE) -f CMakeFiles/eRosita.dir/build.make CMakeFiles/eRosita.dir/application/src/Constant.cc.i
.PHONY : application/src/Constant.cc.i

application/src/Constant.s: application/src/Constant.cc.s

.PHONY : application/src/Constant.s

# target to generate assembly for a file
application/src/Constant.cc.s:
	$(MAKE) -f CMakeFiles/eRosita.dir/build.make CMakeFiles/eRosita.dir/application/src/Constant.cc.s
.PHONY : application/src/Constant.cc.s

help:
	@echo "... application/src/Constant.o"
	@echo "... application/src/Constant.i"
	@echo "... application/src/Constant.s"


At Cmake, we have:

1
2
file(GLOB sources ${PROJECT_SOURCE_DIR}/application/src/*.cc)
file(GLOB headers ${PROJECT_SOURCE_DIR}/application/include/*.hh) */



(so it includes all .hh and .cc)

Is that what you are talking about?
Last edited on
I'm actually not too well versed in make (have only written basic things to get me by), but I don't see where that actually links together the object files into some end result. Maybe somebody else can look at your build script more closely.

But as a sanity check, let's forget about the verbosity of those makefiles and just do some simple command-line call.s

I am assuming you have some file that has your main function.
g++ -Wall Constant.cc Field.cc main.cc -o prog
(Replace paths as appropriate, and add -I include paths if needed).
I have not worked with Cmake, specifically. But I do have some experience with Makefiles (gnu make, I believe). Maybe Cmake is responsible for some of the problems I see, so I apologize if speak out of ignorance.

1: You appear to be somewhat of a beginner, at least in the use of make. So, why are you concerning yourself with .i and .s files? Unless you have a specific reason to study the preprocessed or assembly code, you only need to worry about compiling object (.o) files and linking them into executables.

2: Why are you calling the generated files .PHONY? These appear to be actual files that are the output of actual compilation steps. Wait ... now I see that these are being built in other directories, so maybe this is an attempt to make sure that those files are built. I think it would be cleaner to create another target (not the name of the actual file), and label it .PHONY. (OK, maybe this one's not critical.) Generally, the compiler generates .o files (not .cc.o files) directly from .cc files. There is no directly obvious reason (at least to me) to specify .cc.o files.

3: The Makefile that you provide only mentions Constant. The error you posted talks about errors linking Field.cc.o. Where is the Makefile that does that link?

4: The target Constant.o depends on Constant.cc.o, but there is no rule on how to make one .o file from another. (Oh wait ... that's probably where the .PHONY comes in. Ugh. Maybe this is a Cmake thing, but I am not a fan.)

5: The Makefile never says which .o files should be linked together to create an executable. That seems to be done somewhere else.
Topic archived. No new replies allowed.