0

I have 3 files: - main.cpp - other.cpp - other.h

I am trying to call a function that was declared in other.h, from main.cpp, with the code being written in other.cpp

My code:

main.cpp:

//#Allmyincludes
#include "other.h"

int main(int argc, const char *argv[])
{
    const char *file = argv[1];
    read(file);
    return 0;
}

other.cpp:

//#Allmyincludes
#include "other.h"

void read(const char *file)
{
    //code
}

other.h:

void read(const char *file);

The error I get is:

main.cpp:(.text+0x44): undefined reference to 'read(char const*)'

Within read(), I am using main::variableName to access variables from within main (no errors), however I just can't get the function call to work. If I try to do other::read(file); that also doesn't work because :: is for functions and not files. It gives me the error: 'other' has not been declared.

Any explanation as to why my header file/call isn't working is greatly appreciated.

1
  • Show your compilation command. If using GCC (i.e. g++) notice that order of program arguments to the compiler matters a big lot. Commented Oct 11, 2013 at 14:04

2 Answers 2

1

There are many causes of undefined reference errors. In your case, from what you've described, the likeliest is that you haven't compiled other.cpp

You need to add other.cpp to your project/makefile/command line.

For more detailed help you probably need to explain how you are building your program. There is nothing actually wrong with the code, it's the way you build your program that is the issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I only had g++ main.cpp -Wall -o main
0

try

gcc -Wall main.cpp other.cpp -o mymain

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.