file handling

Hi,

I was under the impression that, when one uses a project in an IDE, it is sufficient to "add file to project" all the needed header files and .cpp files of functions called in main. However, my IDE (Code::Blocks) states "no such file or directory" upon compilation of my #include "header.h" lines, where the header is in another folder.

Is it really necessary to put all the .h files in the same folder of the project? Then one woud ultimately have more than one copy of each .h file on the computer, which would be bad...
What about the .cpp files?

Regards!


edit: ok, I figured out one can include the .h files with the absolute path to the file.
The program compiles, but it terminates after 0s runtime without doing anything (no error).
Is it missing .cpp files, do I have to put them in my project folder?
Last edited on
if its compiling and running without error, its probably/possibly all there. At the very least it was able to resolve everything it uses.

run it from the command prompt / shell and see if there is any output at all, or redirect it to a file if you prefer.

you may need to tell the IDE where to look for things. You don't have to put them anywhere, but you do have to the the IDE where to look.
It gives:

"cpp:17: undefined reference to `random_positions_hard_spheres(int, double, double)'
collect2.exe: error: ld returned 1 exit status"

The header of this function is #included via absolute path, and in the folder of the header is also the respective .cpp file. =/
Sorry if I'm not comprehending correctly, but did you add the .cpp file to the CodeBlocks project?

Also, in general avoid absolute paths in your source code. A better more portable method would be to have the compiler search through directories. This is done with "-I/data[...]/include" commands for g++, and should be an option in the build section of your project in Code blocks.

After doing this, you also may need to change the "path/file.h" to <path/file.h>
after you do "-IC:/data/my_include_dir"

Last edited on
Hey Ganado,

yes, I "added" them in Code Blocks. In the project toolbar, it even shows the folder where the .cpp file is being stored.
Just adding my code:

main:
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
#include <iostream>
#include "C:\Users\Rene\Desktop\Computational Physics\Project\Program\phase_diagram_N_hard_spheres\particle.h"
#include "C:\Users\Rene\Desktop\Computational Physics\Project\Program\phase_diagram_N_hard_spheres\random_positions_hard_spheres.h"
#include <vector>
#include <fstream>

using namespace std;

int main()
{
    int N=200;
    double L=7.7;
    double R=0.5;

    vector <particle> config;
    config=random_positions_hard_spheres(N,R,L);
   
    ofstream coord("random_positions_hard_spheres_N="+to_string(N)+"_R="+to_string(R)+"_L="+to_string(L)+".dat");
    for (size_t i=0;i<config.size();++i){

        coord<<config[i].x*L+L/2<<" "<<config[i].y*L+L/2<<" "<<config[i].z*L+L/2<<endl;
    }


    return 0;
}


random_positions_hard_spheres.h
1
2
3
4
5
6
7
8
9
#ifndef RANDOM_POSITIONS_HARD_SPHERES_H_INCLUDED
#define RANDOM_POSITIONS_HARD_SPHERES_H_INCLUDED

#include <vector>
#include "particle.h"

std::vector <particle> random_positions_hard_spheres (int N, double R, double L);

#endif // RANDOM_POSITIONS_HARD_SPHERES_H_INCLUDED 



random_positions_hard_spheres.cpp
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
32
33
34
35
36
37
38
39
#include <vector>
#include <random>
#include <iostream>
#include "particle.h"
#include "hard_sphere_overlap_index.h"
#include "random_positions_hard_spheres.h"

using namespace std;

//this function places N hard spheres of radius R on random coordinates in a [-0.5,0.5]^3 box
vector <particle> random_positions_hard_spheres (int N, double R, double L){

    vector <particle> positions;

    static seed_seq seed_sequence { 100, 78639, 193, 55555, 3, 2348089, 6474367, 264441578 };
    static mt19937 gen (seed_sequence);
    uniform_real_distribution<double> random_double(-0.5, 0.5); //uniform distribution of real numbers in [-0.5,0.5]

    double x,y,z;
    double d=2*R/L;

    for(size_t i=0; i<positions.size(); ++i){

        x=random_double(gen);
        y=random_double(gen);
        z=random_double(gen);

        positions.push_back({x,y,z});

        //find position with no overlap
        while(overlap_index(positions, i, d)){
            positions[i]={random_double(gen), random_double(gen), random_double(gen)};
        }
        

    }

    return positions;
}


edit: oh my gawd, I'm stupid....... the loop runs over an empty vector......
sorry guys, that might be the issue ~~
Funny that one doesn't realize the error when the code is portrayed in large on the screen, but after one has posted it in a forum...
Last edited on
Topic archived. No new replies allowed.