0

This seems like an straightforward question, but I looked all over and could not find a solution.

So I have 2 .cpp files: a main file, and a function library that the main file needs to see. For various reasons, I do not wish to make a header file for the function library. I do, however, have a header file containing several constant variables that both .cpp files need to see.

It was to my apparently incorrect understanding that the way I would link these 3 files together is by having both cpp files "include" the header file. When I do this, however, main does not have access to the functions in the library. If I add an additional "including library" line to main, I get "error LNK2005". If I try something like having main "include" the library, and have the library "include" the header I also get "error LNK2005".

So... how can I make this set-up work?

aredscout
  • 33
  • 1
  • 9
  • 3
    What are these "various reasons" for not wanting a header file for your functions.cpp? – OJFord Sep 04 '14 at 18:25
  • Vaguely related: http://stackoverflow.com/questions/25671850/error-lnk2005-quest-treeenter-oneclass-quest-treequest-node-class-std – πάντα ῥεῖ Sep 04 '14 at 18:27
  • And please use forward declarations as much as possible to avoid lots of dependencies – Ed Heal Sep 04 '14 at 18:27
  • I'm doing a school assignment- the instructions are pretty specific about what files to make, including exactly what to type into our linux server to compile and run files (including the filenames), and nowhere is it stated or implied that this library's header file should have to be created. Plus, I thought this would be a good thing know, anyway. – aredscout Sep 04 '14 at 18:30
  • "pretty specific about what files to make" - I bet it doesn't say "two .cpp files and no .h files"? – OJFord Sep 04 '14 at 18:33
  • You're right, it is certainly not specifically stated that there CAN'T be .h file for the library. That's just the impression I got. The assignment states at one point: "Put the implementation of the main ( ) routine in your source file prog2.cc, and the implementations of your subroutines, as described below, in your source file sub2.cc". Later it states "To compile the source file of the driver program prog2.cc and the source file of your subroutines sub2.cc, execute: compile.340 prog2 sub2. This will create the object files prog2.o and sub2.o". – aredscout Sep 04 '14 at 18:39
  • But if it's not really possible to do what I was trying to do then perhaps it's expected I make a .h file for the library after all. – aredscout Sep 04 '14 at 18:41

2 Answers2

1

The LNK2005 error means that a symbol is defined multiple times.

This can be related to your "header file containing several constant variables" which breask the one definition rule.

If you have for example in your header a definition like:

int MYCONST = 20;     // variable

It will be defined in both compiled cpp files. When linking these together, your linker will notice that there are two definiitions for the same object.

You have to solve this by declaring the variable in the header (without defining it):

extern int MYCONST;   // declaration only.  Definition somewhere else

and define it only in one of the file (for example in your function library).

Or better, defining in the header as a real constant:

const int MYCONST =20;   // constant (does not offend odr rule)

The one definition rule applies also to function definition (including member functions that would be defined in a class definition in the header).

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thank you for the response. Unfortunately, however, with this assignment, the header has to be exactly what it already is, because it's a saved file on the assignment server that I don't have access to. It just "#defines" several values. – aredscout Sep 04 '14 at 18:55
  • Ok ! If the header contains only #defines, it's not ODR issue (the linker doesn't see the defines). If you go for #including the library into main.cpp, then you MUST just compile main (it will include the code for library) and NOT the library.cpp. Otherwhise everything in library.cpp would be defined twice (once included in main.cpp, and once in the library itself). – Christophe Sep 04 '14 at 20:53
0

That header needs to declare the functions defined in your 'function library' in order for main.cpp to see and use them.

It's not sufficient for the two files to simply share some common include.

Typically, that might look something like this:

main.cpp:

#include "functions.h"

int main(){
    int a = 2;
    char b = myFunction(a);

    return 0;
}

functions.cpp:

#include "functions.h"

char myFunction(int num){
    return 'a'+num;
}

functions.h:

char myFunction(int);

Otherwise, when compiling main.cpp there is no way to check the type of b is correct - we wouldn't know what to expect from myFunction. We also wouldn't know if we were supplying the correct types as parameters - or even the correct number of them!

OJFord
  • 10,522
  • 8
  • 64
  • 98