C Programming Linker Error Undefined Reference To
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more linker error undefined reference to winmain@16' dev c++ about Stack Overflow the company Business Learn more about hiring developers or posting ads linker error undefined reference to function in c with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow linker error undefined reference to __chkstk_ms' is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What causes the error “undefined reference to (some function)”? [duplicate] up vote
Linker Error Undefined Reference To Wsastartup@8'
7 down vote favorite This question already has an answer here: What is an undefined reference/unresolved external symbol error and how do I fix it? 25 answers I get the error: main.o(.text+0x1ed): In function `main': : undefined reference to `avergecolumns' collect2: ld returned 1 exit status when I gcc *.o. I'm not quite sure what causes this error. Other posters have explained it as the function is not found linker error undefined reference to __cpu_features_init or the function is empty. If someone could clarify or refine, it would be greaty appreciated! Here is my function's code(I'm trying to calculate the average of the column in 2D arrays): #include "my.h" void averagecolumns (int x, int y, int** a) { int i; int j; float sum; float colAvg; sum = 0; colAvg = 0; printf("i. The column averages are: \n"); for(i = 0; i < x; i++) { for(j = 0; j < y; j++) { sum += a[i][j]; colAvg = sum / (float)x; } printf("Column: %3d, Average: %6.2f", j, colAvg); sum = 0; colAvg = 0; } The relavent parts of main are: #include "my.h" int main (int argc, char* argv[]) { int** a; float** colAvg; int ROWS; int COLS; int i; int j; int** table; FILE* fpmyfile; int closeResult; .... printme (ROWS, COLS, a); // call functions a - j oddvalues (ROWS, COLS, a); oddlocations (ROWS, COLS, a); countoddrows (ROWS, COLS, a); addrows (ROWS, COLS, a); findfirstsmall (ROWS, COLS, a); findlastlarge (ROWS,COLS, a); addcolumns (ROWS, COLS, a); avergecolumns (ROWS, COLS, a); .... } Also, is this a linker or a compile error (I wasn't sure which tag to add). c linker-error share|improve this question edited Nov 6 '11 at 0:49 Kerr
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn undefined reference error in c more about Stack Overflow the company Business Learn more about hiring developers or
Undefined Reference To Function C++
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community
Undefined Reference Error In C++
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Undefined reference to '_*' linker error up vote 0 down http://stackoverflow.com/questions/8024440/what-causes-the-error-undefined-reference-to-some-function vote favorite I am having trouble compiling/linking the following C code. The linker throws errors that look like the following: pso.o:pso.c:(.text+0x41): undefined reference to '_ps' ... pso.o:pso.c:(.text+0x93): more undefined references to '_ps' follow This is my first time writing C code for gcc, so I'm unsure how to fix this problem. I am assuming that because struct PS is defined my header file, it is somehow not linked http://stackoverflow.com/questions/15257120/undefined-reference-to-linker-error to pso.c. However, I did use a #include "ps.h" statement at the top of that source file. I have included the relevant source files and header file below, as well as the make file I am using. Is there a fundamental concept I am missing for writing linkable C code? Thank you! Oh, and it's a particle swarm optimizer, if you were wondering :) The main.c file: #define MAIN #include here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the http://stackoverflow.com/questions/26820736/linking-error-undefined-reference-to-functions-thatre-defined-in-a-separate-fi company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million https://latedev.wordpress.com/2014/04/22/common-c-error-messages-2-unresolved-reference/ programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Linking error: Undefined reference to functions that're defined in a separate file? up vote 1 down vote favorite I'm probably undefined reference forgetting something obvious that'll solve this. While there's other questions on SO with the same issue, none of the solutions have been applicable to my situation. I have a main file, sim.c, a header file net.h, a header file friends.h, and a file with the functions, net.c. I have a makefile, which I created with gmakemake > Makefile, and its contents are very basic. Header.mak (makefile template): CFLAGS = -Wall -Wextra -pedantic -std=c99 -ggdb undefined reference to LFLAGS = -ggdb LDFLAGS = Makefile relevant contents: CPP_FILES = C_FILES = net.c sim.c PS_FILES = S_FILES = H_FILES = net.h friends.h SOURCEFILES = $(H_FILES) $(CPP_FILES) $(C_FILES) $(S_FILES) .PRECIOUS: $(SOURCEFILES) OBJFILES = # # Main targets # all: net sim net: net.o $(OBJFILES) $(CC) $(CFLAGS) -o net net.o $(OBJFILES) $(CLIBFLAGS) sim: sim.o $(OBJFILES) $(CC) $(CFLAGS) -o sim sim.o $(OBJFILES) $(CLIBFLAGS) # # Dependencies # net.o: net.h sim.o: net.h My sim.c file contains: #include "net.h" #include "friends.h" My header file contains the functions in net.c and defines them all as stubs. I copied and pasted them to create the function headers, so there shouldn't be any typos. My net.c file contains: #include "net.h" Yet any time a function in sim.c tries to call a function in net.c, it errors on that line with: "undefined reference to `function_name`". How can I make sim.c able to access the functions in net.c? c makefile share|improve this question edited Nov 8 '14 at 19:04 asked Nov 8 '14 at 18:44 Rohawk 11210 add a comment| 2 Answers 2 active oldest votes up vote 0 down vote accepted The message undefined reference to 'function_name' implies that of all the object files you're giving to the linker, none of them has a definition for function_name. That means that either You're not linking with net.o net.c error message (or “unresolved external symbol, for Visual C++ users). This is not actually a message from the compiler, but is emitted by the linker, so the first thing to do is to understand what the linker is, and what it does. Linker 101 To understand the linker, you have to understand how C++ programs are built. For all but the very simplest programs, the program is composed of multiple C++ source files (also known as “translation units”). These are compiled separately, using the C++ compiler, to produce object code files (files with a .o or a .obj extension) which contain machine code. Each object code file knows nothing about the others, so if you call a function from one object file that exists in another, the compiler cannot provide the address of the called function. This is where the the linker comes in. Once all the object files have been produced, the linker looks at them and works out what the final addresses of functions in the executable will be. It then patches up the addresses the compiler could not provide. It does the same for any libraries (.a and .lib files) you may be using. And finally it writes the executable file out to disk. The linker is normally a separate program from the compiler (for example, the GCC linker is called ld) but will normally be called for you when you use your compiler suite’s driver program (so the GCC driver g++ will call ld for you). Traditionally, linker technology has lagged behind compilers, mostly because it’s generally more fun to build a compiler than to build a linker. And linkers do not necessarily have access to the source code for the object files they are linking. Put together, you get a situation where linker errors, and the reasons for them, can be cryptic in the extreme. Undefined reference Put simply, the “undefined reference” error means you have a reference (nothing to do with the C++ reference type) to a name (function, variable, constant etc.) in your program that the linker cannot find a definition for when it looks through all the object files and libraries that make up your project. There are any number of reasons why it can’t find the definition – we’ll look at the commonest ones now. No Definition Probably the most common reason for unresolved reference errors is that you simply have not defined the thing you are referencing. This code illustrates the problem: int foo(); int main() { foo(); } Here, we have a declaration of the function foo(), which we call in main(), but no definition. So we get the error (slightly edited for clarity): a.cpp:(.text+0xc): undefined reference