Visual C ++ unresolved external character (cannot find one of my own functions)

This is a very simple problem that disappoints me today. Say, in the framework of one solution, I have two projects. Call the first SimpleMath project. It has one header file "Add.h", which has

int add(int i, int j) 

and the implementation of "Add.cpp", which has

 int add(int i, int j) { return i+j; } 

Now let's say in the second project, I want to use the add function. However, this code:

#include ".. \ SimpleMath \ Add.h"

 int main() { add(1, 2); } 

results in an "unresolved external character". How to make the second program "know" about the actual implementation in the .cpp file. As a side note, all the code is fictitious; this is not how I actually program.

+4
source share
7 answers

The reason for the error you get is that by including the header file that you are telling the compiler, there is a character

 int add (int, int) 

This will be present during the binding, but you have not actually included this symbol (code for the function) in your project. A quick way to solve the problem is to simply add Add.cpp to both projects. But the β€œnice” solution would probably be to make SimpleMath in the library instead of the application, changing the project type in the project properties.

And by the way, you probably need some mechanism to prevent multiple inclusion of this header file in place. I usually use #pragma once , which should be fine if you stick with Visual C ++, but it may not be completely portable, so if you want to port, go on to a more traditional approach to wrapping the header file in #ifndef -block, as such

 #ifndef __ADD_H #define __ADD_H int add (int i, int j); #endif 

Good luck.

+5
source

If you are trying to link C libraries with C ++ projects, you need to do something like

 extern "C" { #include "..\SimpleMath\Add.h" } 

since C and C ++ use different symbol names.

+5
source

You either need to make the Add.cpp part of the library part, or include it in both projects. or you need to add Add.cpp to the second project.

Edit: make the SimpleMath library go to the project settings in the "General" section and change the configuration type to "Static Lib".

Then go to the settings of your solution, click "Project Dependencies", then select your second project from the drop-down list and check the box next to SimpleMath. This will automatically connect SimpleMath to your second project and will also verify that any changes to SimpleMath will be restored when you restore your second project.

+3
source

SimpleMath must be defined as a library file (.LIB) in its project properties. I assume this is unmanaged (not .Net) C ++. Then include SimpleMath.lib in another project.

+1
source

Take a look at this link, this should help you:

http://msdn.microsoft.com/en-us/library/0603949d.aspx

0
source

I had this problem inside the same project ... After carefully examining my code, I notice that the code trying to call the function was using the interface (via a clean virtual method call = 0). However, I forgot to add a β€œvirtual” word in the interface class and implementation class. When I added "virtual", the problem was resolved.

0
source

It was just that he was in the same project, and it turned out I had two filters and named two .cpp files with the same name . Thus, Visual Studio simply overwrites one .obj when compiling another. The result is a lack of functions in the first .obj. Lesson: never call .cpp files the same, even in different folders / filters.

0
source

Source: https://habr.com/ru/post/1277603/


All Articles