Linker Error, Compiling Multiple Source Files

Hi, LeafyCircuits here!

Comp Specs:
OS: Windows 7 Home Premium 64-bit
Compiler: Nuwen's Distro of MinGW v4.6.2 containing Boost Libraries.
IDE: Code::Blocks v12.11

So I haven't made a project with multiple source files in a while, so I'm probably making an obvious error that I can't see. What I have below is an experiment project containing a main.cpp, test.cpp and test.h, and binaryfiles.h, which I created for working with basic Binary File IO. The problem occurs when I include binaryfiles.h in test.h, then include test.h in test.cpp. If I include binaryfiles.h twice in main.cpp, I get no error, which throws me off. Here are the files:
main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "binaryfiles.h"
#include <curses.h>

int main()
{
    initscr();

    getch();

    endwin();
}


test.cpp
#include "test.h"

test.h
1
2
3
4
5
6
#ifndef TEST_H
#define TEST_H

#include "binaryfiles.h"

#endif // TEST_H 


binaryfiles.h
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#ifndef BINARYFILES_H
#define BINARYFILES_H

#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <cstring>

using namespace std;

int binaryRead(string filename, char *bits, int len)
{
    ifstream infile(filename.c_str(), ios_base::binary);
    if (!infile.good() || len < 0)
    {
        infile.close();
        return 1;
    }

    if (len == 0)
    {
        struct stat results;
        stat(filename.c_str(), &results);

        infile.read(bits, results.st_size);
    }
    else
    {
        infile.read(bits, len);
    }

    infile.close();
    return 0;
}

int binaryWrite(string filename, char *bits, int len)
{
    ifstream infile(filename.c_str());
    if (!infile.good() || len < 0)
    {
        infile.close();
        return 1;
    }

    infile.close();
    ofstream outfile(filename.c_str(), ios_base::app | ios_base::binary);
    if (len == 0)
    {
        outfile.write(bits, strlen(bits));
    }
    else
    {
        outfile.write(bits, len);
    }
    outfile.close();
    return 0;
}

int binaryRewrite(string filename, char *bits, int len)
{
    ifstream infile(filename.c_str());
    if (!infile.good() || len < 0)
    {
        infile.close();
        return 1;
    }

    infile.close();
    ofstream outfile(filename.c_str(), ios_base::trunc | ios_base::binary);
    if (len == 0)
    {
        outfile.write(bits, strlen(bits));
    }
    else
    {
        outfile.write(bits, len);
    }
    outfile.close();
    return 0;
}

#endif // BINARYFILES_H 


So I have two theories as to why this is giving me problems:

1.
When I included another header file in place of binaryfiles.h that had a class declaration instead of the function declarations you see there, it compiled fine. So maybe I'm just declaring things wrong.

2.
As you see from main.cpp, I am using <curses.h>, which means I had to change some things with the linker in order for the libraries to compile correctly. Here's what's going on:

I have 2 extra linker options
they are -lgdi32 and -lcomdlg32. If I don't include these options, I get reference errors in the respected libraries.

I have one static library being linked
That is a file called pdcurses.a. I need this as well, obviously.

Order of Linker options is different
I've discovered that my Linker options were flopped, and because of that, PDCurses wouldn't Link. Here's what's being invoked, and what changed:
g++.exe  -o test.exe "E:\...\main.o" "E:\...\test.o"   E:\...\pdcurses.a -static-libgcc -static-libstdc++ -lgdi32 -lcomdlg32


(the elipses I added to shorten filename, but actuall command contains full filename). The linked library and link options were flopped. Code::Blocks allows you to define how it invokes the compiler and linker through macro "templates", so the macros name as $libs and $link_options were flopped.


Getting kind of frustrated by this thing, since I've pulled this off before. If anybody would reply, I would really appreciate it. If you need more information to my plight, just ask.
Header files do not protect against redefinitions. If you define your function in a header file, your different source files will each have its own definition of your function.

Move your definitions in binaryfiles.h to binaryfiles.cpp.

Random Notes:
-I question your need for <iostream> in binaryfiles since you don't use anything from that header.
-I also question the need to have a using namespace declaration in a header file, which will affect all source files that include it.
Daleth said:
-I question your need for <iostream> in binaryfiles


I'm using string variable types, doesn't that need <iostream>, or is it defined along with int, char, etc.?
#include <string>
all right. Thanks Daleth :)
Topic archived. No new replies allowed.