0

using: Visual Studio 2008

Question:

I have a library math.lib which has functions like this:

#include <cmath> 
#include<stdio.h>
#include <conio.h>

int addNumbers(int a, int b)
{
    return a + b;
}

And my main function is: (Updated)

#include<stdio.h>

void main()
{
    printf("show: %d", addNumbers(3,5));
    getchar();
}

I have included the path and link input in the Visual Studio. But I get an error:

error C3861: 'addNumbers': identifier not found

Can anyone help me how to solve this?

Shereif SRF
  • 58
  • 1
  • 12
  • How would you expect your main function to know about another function? Would it know about `printf` if you didn't include stdio.h? You need to provide/include a header with your function declaration in it. – Retired Ninja Jul 19 '17 at 03:16
  • Is `math.lib` the library you create? If so, don't include it in the compilation of the source files — only use it in the linking command line. You need a header such as `mathlib.h` containing the function declarations. You'll use it in the file with `main()`, and also in the file(s) that define the functions. You'll probably build the file(s) containing the functions into the maths library (`math.lib`), and then link that on the command line along with (and after) the object file containing `main()`. – Jonathan Leffler Jul 19 '17 at 03:44
  • @MohammedShereif -- *would there be anything wrong at my library building part?* -- The extension `.lib` is used by most programs written for the Windows environment to mean an external import library or static library, **not** a C++ header file. Thus your choice of names is confusing. – PaulMcKenzie Jul 19 '17 at 04:13
  • I'm sorry about it. By the way. If my math.lib is working fine then how does an error come? I have linked the path and the library name into the project linker. – Shereif SRF Jul 19 '17 at 04:17
  • @MohammedShereif -- Then you're confused as to how a C++ program is built. Is that a C++ header file? If so, then it is not involved in the linking process. – PaulMcKenzie Jul 19 '17 at 04:19
  • First of all, In my `math.lib`, I have two files. One is `.cpp` and the other is `simplemath.h`. I included the `simplemath.h` in the new project's `main()` – Shereif SRF Jul 19 '17 at 04:20
  • Yeah, its a C++ header file – Shereif SRF Jul 19 '17 at 04:21
  • Do you know what `#include` does? From what you've described, I don't think so. Why are you including a `.cpp` file? The `#include` directive does nothing more than paste the code from that file into your program. It doesn't do any "importing" or anything like that (if you are used to Java, C#, or similar languages). – PaulMcKenzie Jul 19 '17 at 04:24
  • I know how it works. I included it because someone told me in the answer. What I did as my own is, Just properties->Linker-> General (the path of the math.lib and properties->Linker->input(the name of the .lib file. And I got an error (shown in the question) – Shereif SRF Jul 19 '17 at 04:27
  • @MohammedShereif No. Again, a C++ header file is not involved in the linking process -- it is used solely in the compilation process. If anything, you change the compiler's include path for header files, not linker paths. – PaulMcKenzie Jul 19 '17 at 04:28
  • @PaulMcKenzie... How should I include my library then Sir? Would you mind to share it? – Shereif SRF Jul 19 '17 at 04:31
  • It isn't a library. You just have a source file. You change the compiler's include path for header files. – PaulMcKenzie Jul 19 '17 at 04:32
  • Sorry, My bad its not a header file. It's a **C++ library file** (math.lib) – Shereif SRF Jul 19 '17 at 04:32
  • Then you don't `#include` it. That goes back to my previous point. Why are you including object code in your source file if it is a library? – PaulMcKenzie Jul 19 '17 at 04:33
  • I didn't include it. I just tried it as one of the people in the answer comments told me to. ( I have edited the question already) – Shereif SRF Jul 19 '17 at 04:34
  • Take that .lib file and open it in a text editor. Is it source code or is it gibberish? If it's gibberish, then it is object code, and should not be included in anyway in your source modules. When compiling your program, the only thing the compiler cares about is if you've declared the functions you're calling (which you failed to do). It doesn't care if the function really exists. When it comes to the link phase, the linker now will search your object code, and if not there, any specified external libraries for the actual functions. – PaulMcKenzie Jul 19 '17 at 04:37
  • It's gibberish. I didn't include anything other than `stdio.h` and I linked the path in the Linker. – Shereif SRF Jul 19 '17 at 04:40
  • That's the problem. C++ must have the functions declare / prototyped before you refer to them. You failed to declare your functions. All of those other header files you've included, such as `` contain function declarations for the I/O functions you're calling. – PaulMcKenzie Jul 19 '17 at 04:41
  • So I have to add `int addNumbers(int, int);` right? – Shereif SRF Jul 19 '17 at 04:43
  • Yes, but it must have a semicolon at the end. Now, that line you would put in a header file and #include it, so that you're not typing `int addNumbers(int, int);` over and over again every time you want to use it in a source module. – PaulMcKenzie Jul 19 '17 at 04:45
  • Its working. Thank you very much, sir. Could you please post it as an answer for others? @PaulMcKenzie – Shereif SRF Jul 19 '17 at 04:46

2 Answers2

2

The issue is that you failed to declare or prototype the functions that you're calling in your source module.

C++ requires you to either declare your functions, or have the the entire function body appear before any usage of that function. So for your case, it would be:

#include<stdio.h>

int addNumbers(int, int);

int main()
{
    printf("show: %d", addNumbers(3,5));
    getchar();
}

Note that usually, you would put function declarations and prototypes in a header file, and include that header file. Assume the name of your header file is "mylib.h":

#ifndef MYLIB_H
#define MYLIB_H

int addNumbers(int, int);

#endif

Then

#include<stdio.h>
#include "mylib.h"

int main()
{
    printf("show: %d", addNumbers(3,5));
    getchar();
}

The compiler doesn't actually care if the function actually exists, just as long as you've declared it before making a call to the function.

During the link phase, the linker will now go search for the functions that you're calling, either in the object code your compilation has produced, or in an external library that you would have specified to the linker.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
-1

By the looks of it, you may have forgotten to include your math.lib class into your main code file.

Try this:

#include <stdio.h>
#include "math.lib"    // New line: Quotes tell compiler that it is not a standard library

/* Rest of main code */

Now the compiler will know how to define math.lib and the functions inside of it.

EDIT: Also make sure that your math.lib source is in the same directory as the main source or replace math.lib with the file path in the #include directive.

spicy.dll
  • 948
  • 8
  • 23
  • I have tried it. But there come another error `fatal error C1083: Cannot open include file: 'math.lib': No such file or directory` – Shereif SRF Jul 19 '17 at 03:25
  • Turns out I'm only half right. Visual Studio does have an `#import`. [Looks like in visual studio](https://msdn.microsoft.com/en-us/library/8etzzkb6.aspx) you can `#import "math.lib"` to get it to link, but you'll still need `#include "something.h"`to get the header included for the declarations for the stuff in math.lib. – user4581301 Jul 19 '17 at 03:27
  • find the source file for `math.lib` and move it to the same directory as your main source @MohammedShereif – spicy.dll Jul 19 '17 at 03:27
  • @user4581301 Ya I was about to say that. I must have been using visual studio to much or something – spicy.dll Jul 19 '17 at 03:28
  • Sorry about that. I'll leave the comments so I can look like a fool in public. – user4581301 Jul 19 '17 at 03:29
  • Yeah. I did it. Now I'm getting 8 errors. I will update the errors in the question – Shereif SRF Jul 19 '17 at 03:33
  • @MohammedShereif **Now I'm getting 8 errors** Yeah that's life for ya – spicy.dll Jul 19 '17 at 03:34
  • can you help me fix those too? I have updated my question with the error details – Shereif SRF Jul 19 '17 at 03:38
  • Yeah. I have Updated it. – Shereif SRF Jul 19 '17 at 03:41