This question already has answers here:
Undefined symbols error when using a header file
(2 answers)
Closed 4 years ago.
I have some useful functions in another project (project 2) but do not want to copy and paste all of them into a file within the project i'm currently working on (project one). I've tried making and including a header file within project one but it hasn't worked. Do I have to copy paste project 1 into project 2? My IDE is codelite.
Thanks<3
EDIT: my header file is called hewwo.h and the code is
extern int readln(char[], int);
extern int searchstring(char[], char[]);
this file is within project 1.
and at the top of main.c in project 1 I have
#include < stdio.h> #include <stdlib.h> #include <string.h> #include "hewwo.h"
I'm trying to use the readln function in main.c and its throwing an "undefined symbols" error
The reason you're getting the error is because with the function declarations in the header file you're telling the compiler this function exists somewhere and it doesn't care where. Thus it compiles, but the links to the actual function implementations aren't there. You need to bring in the implementations via a static or dynamically linked library.
Related
This question already has answers here:
undefined reference to `WinMain#16'
(7 answers)
Closed 2 years ago.
problem in .exe file of program_1. It is not creating a executable file after typing "gcc program_1.c -o program-1".It is showing the following thing in the picture but for main.c ,it had created the executable file .So please resolve my problem.
The reference to WinMain#16 means that you are linking as a Windows application, not a Console application. I don't use MINGW, but you should check how to set the application type.
I see it returns a 1. When coding using C, a preferred practice is to include this line of code at the end of the main:
return 0;
With the purpose of telling the OS that your program executed successfully. Also you don't have a return type to your main, you can choose between depending if you want to return an int or nothing:
int main()
void main()
This question already has an answer here:
VXWorks adding functions to header files
(1 answer)
Closed 2 years ago.
I want to add functions to the default vxworks Ball DKM project. I've copied the style I see for all the functions that I am able to call from a given header file and declared my own in master/vxworks-6.9/target/h/wrn/coreip/netBufLib.h:
EXTERN void hello(void); //also tried EXTERN void hello();
I then added the body in the only corresponding source file with the same name (master/components/ip_net2-6.9/vxmux/src/mem/netBufLib.c)
void hello() {
printf("hello, world from netbuflib.c!");
}
At project build time I get the warning:
On downloading module '/ball/SIMLINUXdiab/ball/Debug/ball.out' on target vxsim2_0, the module symbols could not be fully resolved
Unresolved symbols list: hello
Do you want to continue launch and ignore...
If I proceed, naturally a seg violation occurs when I call hello().
Is there a linking step or something I have to do besides clean the project and rebuild it because of Eclipse/VXWorks? Or is it maybe related to using SIMLINUX or a target vxsim simulator? Finally I suppose it could be that the source file is not the correct corresponding source code, even though it is the only same named one in this directory (VXWorks repo could only come with compiled assembly)?
Since you are trying to define the function hello() in DKM you should add the definition in some source file related to the DKM project(not in the core libraries) and add the declaration in the source file where you are calling hello then rebuild and run it.
Or, if you want to add the function in core libraries(as you have done) you have to rebuild the library and add the declaration of the function in the file where you are calling the hello.
Is using a function defined in another file (example.[ch]) which is placed in project directory (and included with #include "example.h") needs editing project Makefile?
i tried using my function in part of net-snmp project and faced linker errors.
the error is:
./.libs/libnetsnmpmibs.so: undefined reference to `snmpget'
the nodeFunc.c look like this:
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <string.h>
#include "NodeFunc.h"
int snmpget(char* remoteip, short remoteport,char* community,int *len,char* str)
{
....using some net-snmp functions to get a certain object from a remote host....
}
and i used the snmpget function in my main file like this:
...
snmpget(remIP,161,remComm,&lenth,ansstr);
...
It is not the addition of a function that would require modification on the makefile, but rather the addition of the source file itself. If in the makefile the source file is not already referenced as a compilation target and its object file is not a link dependency, then it will not be included in the build.
Inclusion in the build is not the purpose of the #include directive. That merely includes the content of the .h file in the source file in order that the compiler has visibility of the declarations; it does not involve the .c file containing the definitions - that must be separately compiled and linked which is the role of the makefile in this case.
This question already has answers here:
What is the difference between #include <filename> and #include "filename"?
(30 answers)
Difference between angle bracket < > and double quotes " " while including header files in C++? [duplicate]
(2 answers)
Closed 9 years ago.
I would like an explanation of the difference between < header.h > and "header.h" in library #include directives. How exactly (in which locations) does the linker search for the files? In what order does it perform the search?
When we write <stdio.h> , we are referring to a header file which is available in the include directory of the system. When we write #include <stdio.h>, the preprocessor searches for the header file in the system include directory and not in the current directory. When we write #include "stdio.h", the preprocessor starts searching for this header file in the current directory and then in its parent directories. So if we write our own stdio.h, save it in the current directory, and include it in the program using #include "stdio.h" then our header will be included instead of the system header.
In short, if we use angular brackets (<>) then we are indicating that the file can be found in one if the standard directories in the
system. If we use quotation marks (" ") then we are indicating that a non-standard header is being used.
I have been having these really odd problems with Visual Studio 2010. At this point, the behavior is so erratic that I really wish I did not have to use it for CUDA (I know I don't have to, but it is hard not to use it).
One of the many problems I have been having with really basic stuff is header files being included more than once. For example:
//vars.cuh
#if !defined(VARS_cuh)
#define VARS_cuh
#include <cuda.h>
#include <cuda_runtime_api.h>
int* kern_xstart, *kern_xend, *kern_ystart, *kern_yend, *kern_zstart, *kern_zend;
/* more variable definitions */
#endif
I then include this file in most of my source files:
//source_file.cu
extern "C"{
#include "vars.cuh"
/* more includes of my own headers */
#include <cuda.h>
#include <cuda_runtime_api.h>
}
/* source file body */
The VS 2010 compiler puts out errors like this: "error LNK2005: foo already defined in other_source_file_I_wrote.cu.obj"
Why is it doing this? Also, to kill two birds with one stone, with this setup, I also have problems with writing a function in source_file.cu, and then prototyping it in vars.cuh. The problem arrises that vars.cuh can't see the definition, even though I am clearly including vars.cuh in source_file.cu!
Thank you!
The header file is being compiled multiple times because, as you say, you include this header file in most of your source files. Those global variables are included in multiple source files and thus are defined in every source file that includes the header. When the linker links all of the object files together, it finds multiple definitions of those variables, hence the error.
If you want to share global variables across multiple source files, declare them as extern in the header, then define each of them once in one source file.
This isn't a problem with Visual Studio or the Visual C++ compiler, it's how C works.