I'm trying to configure VS correctly to write some simple c programs.
If a function dummy() is not found, the function name is not highlight in the code, there is only a build error:
LNK2019 unresolved external symbol dummy referenced in function main
Line: 1
Is there a way to make VS intellisense work for function names with .c files?
Why compiler doesn't show the error line number (Line: 1 is not very helpful)?
Sample:
#include <stdio.h>
#include <stdlib.h>
void dummy()
{
}
int main()
{
dummyX(); //dummyX, dummy typo. Results in "unresolved external..." error
return EXIT_SUCCESS;
}
Related
I have looked at every post pertaining the "multiple Lua VMs detected" error and none of their answers worked. I have done everything the lua.org building guide says to compile it and it still shows the error. Using visual studio 2019 causes an unresolved external symbol error and using the GCC command without -llua also causes unresolved symbols. Any ideas. I am using version lua 5.3.2 from the binary compiled with the provided make file.
this is the code I am trying to use
#include "lua.h"
#include "lauxlib.h"
#include <Windows.h>
int test_function(lua_State* L)
{
return 0;
}
static const luaL_Reg testlib[] = {
{"test_function", test_function},
{NULL, NULL}
};
__declspec(dllexport) int __cdecl luaopen_testlib(lua_State* L)
{
luaL_newlib(L, testlib);
return 1;
};
local lib, err = package.loadlib([[module.dll]], "luaopen_testlib")
if lib ~= nil then
--multiple Lua VMs detected
lib()
else
print(err)
end
This question already has answers here:
Linking static function from another file in c
(2 answers)
Closed 2 years ago.
I am getting a linking error when i run my program, even though the function signature is the same for both functions in test.h and test.c :
test.h :
void function(int);
test.c :
#include "test.h"
#include "stdio.h"
static void function(int n) {
printf("%d\n", n);
}
main.c :
#include "test.h"
int main() {
function(5);
return 0;
}
this is the output:
Undefined symbols for architecture x86_64:
"_function", referenced from:
_main in ccNaA2H2.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
In class, I learned that the function signature is the function name and its parameters.
So, why doesn't my program in main call function(5) in test.h which will call the function(5) in test.c ?
Thank you
static at global scope (outside of functions) means that it is only visible in this file (It has internal linkage). So, the static void function(int n) in test.c is not visible from main.c.
For the call function(5); in main, the compiler sees the function prototype in test.h, but the linker cannot find an implementation of it, because the function in the c file is static.
Solution: remove the word static, if you want to use the function in a different file.
In test.c you declared your function as static which means, it will not be visible outside that module. Remove the static keyword.
I am trying to compile a c program with a static library and its not working .
This is the error :
undefined reference to `calculatearea'
collect2.exe: error: ld returned 1 exit status .
The static files were made with the gcc / g++ compilers .
This is the main code :
#include <stdio.h>
#include <stdint.h>
int calculatearea(int a , int b);
int main()
{
int c = calculatearea(2,4);
printf("%d",c);
getchar();
return 0;
}
edit :
: screenshot of compiler error
From the above code we can see that you have declared the function int calculatearea(int a , int b); but have not written any definition for the same. and you are calling this function in the main. compiler is not finding the definition for the function calculatearea and giving error.
To solve this:
1) Write the definition for function calculatearea in the same file.
2) Make use of extern specifier with this function declaration and make sure that definition is present with the link library at the time of compilation.
3) As mentioned in the picture if the area.o have the definition of function calculatearea, then compile as below, this will generate a.out in linux:
gcc filename.c area.o
I have started using gsl recenltly in a huge old C project. I have managed to add the libraries by adding the location in my system in Properties>C/C++>General>Additional Include Directories.
In my code, I am also including the following:
#include "gsl/gsl_matrix.h"
#include "gsl/gsl_matrix_complex_double.h"
#include "gsl/gsl_matrix_complex_float.h"
#include "gsl/gsl_matrix_complex_long_double.h"
#include "gsl/gsl_math.h"
#include "gsl/gsl_spmatrix.h"
#include "gsl/gsl_complex.h"
#include "gsl/gsl_complex_math.h"
#include "gsl/gsl_inline.h"
#include "gsl/gsl_complex.h"
I can now use most functions of gsl. but in the fowllowing function:
void vector_complex_mul_elements(gsl_vector_complex *v1, gsl_vector_complex *v2)
{
gsl_complex cpx1, cpx2, cpx3;
GSL_SET_COMPLEX(&cpx1, 0, 0);
GSL_SET_COMPLEX(&cpx2, 0, 0);
GSL_SET_COMPLEX(&cpx3, 0, 0);
if(v1->size != v2->size)
{
printf("Error: Lenght of arrays do not match.\n");
return;
}
for(i=0; i < v1->size; i++)
{
cpx1 = gsl_vector_complex_get(v1, i);
cpx2 = gsl_vector_complex_get(v2, i);
//cpx3 = gsl_complex_mul(cpx1 , cpx2);
gsl_vector_complex_set(v1, i, cpx3);
}
}
When I uncomment the line:
cpx3 = gsl_complex_mul(cpx1 , cpx2);
I get the following errors:
Error LNK2001: Unresolved external symbol "_log1p".
Error LNK2001: Unresolved external symbol "_log1p".
Error LNK2001: Unresolved external symbol "_hypot".
Error LNK1120: 2 unresolved external references.
I have already tried writing it like:
gsl_vector_complex_set(v1, i, gsl_complex_mul(cpx1 , cpx2));
Then I get these errors:
Error LNK2019: Reference to unresolved external symbol "_log1p" in function "_gsl_complex_logabs".
Error LNK2019: Reference to unresolved external symbol "_hypot" in function "_gsl_complex_div".
Error LNK2001: Unresolved external symbol "_log1p".
Error LNK1120: 2 unresolved external references.
Is this a only a linking problem or the way I am using it is wrong?
These (lop1p and hypot) functions are in the standard maths library. Are you including math.h and linking to it (-lm)? As per the GSL documentation.
It seems to me that you are wrong linked GSL library.
Try to rebuild GSL as a dll and relink it, as I showed you in your other post.
[Edit: The question is flawed, the file I described as "main.c" was actually "main.cpp" and that it why I was having an issue, calling a C function from a C++ file. The question is thus incorrect and doesn't have an answer, but if you have this undefined symbol issue, also think about checking you're not mixing C & C++.]
I'm using uVision 5 to develop a firmware, however I can't get the linker to find one of my functions.
main.c :
#include "Test.h"
int main()
{
return three();
}
Test.h :
#ifndef TEST_H
#define TEST_H
int three();
#endif
Test.c
#include "Test.h"
int three()
{
return 3;
}
All those files are at the root of my project, I know they get compiled as if I introduce a syntax error in them, compiler reports an error.
Also looking at the map file produced, I see that three() was removed:
Removing test.o(i.three), (4 bytes).
For testing purposes, I had --no_remove to linker command line, map file now contains:
0x0002ba76 0x00000004 Code RO 1 i.three test.o
So obviously, the linker is well aware of my function, and will or won't remove it depending on flags.
Regardless, it reports:
.\build\uvision5\test.axf: Error: L6218E: Undefined symbol three() (referred from main.o).
Not enough information to list image symbols.
Flawed question, it was actually a case of mixing C/C++, in which case you'll get a symbol missing if you call a C function from C++ without declaring it extern C.