I've been getting this error numerous times in my program. I've simplified things down a bit to illustrate the basics and am still getting errors. I was told that I needed to add this library file to my project for it to work (libncurses.dylib) and it did solve some problems, but not this one.
Here is my code:
// screen.h
#ifndef screen_h
#define screen_h
#define MAC 1
#define WIN 2
#define LNX 3
#ifdef PLATFORM
#undef PLATFORM
#endif
#define PLATFORM MAC
void screen_erase();
#endif
// screen.c
#include <string.h>
#include <stdlib.h>
#include "screen.h"
#if PLATFORM == MAC
#include <curses.h>
void screen_erase(){
erase();
}
#endif
// main.cpp
#include <iostream>
#include <curses.h>
#include "screen.h"
using namespace std;
int main(){
screen_erase();
}
And here's the error I am getting:
Undefined symbols for architecture x86_64:
"screen_erase()", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation
What's going on here?
It's because you mix two different languages: C and C++.
In the screen.h header file, change the declaration to this:
#ifdef __cplusplus
extern "C" {
#endif
void screen_erase();
#ifdef __cplusplus
}
#endif
That tells the C++ compiler to not do name mangling on the screen_erase function name.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last month.
IDE: VS code
gcc version: 8.1.0 (x86_64-posix-seh-rev0, Built by MinGW-W64 project)
I new in C , now I have three files:
Get_para.h
#ifndef _GETPARA_H_
#define _GETPARA_H_
extern double C;
extern double dEarthAngularVelocity;
...
extern void calParameter();
#endif
and Get_para.c, which is the implement of Get_para.h
#include <math.h>
#define _USE_MATH_DEFINES
#define pi M_PI
double C = 3e8;
double dEarthAngularVelocity = 7.29210e-5;
...
void calParameter(){
...
}
then, I want to include Get_para.h in test.c and call calParameter function which is implemented in Get_para.c
#include <stdio.h>
#include "Get_para.h"
int main(){
calParameter();
printf("%lf\n",dSemiMajorAxis);
}
I use 'run code' in VS,the command in terminal is:
if ($?) { gcc test.c -o test } ; if ($?) { .\test }
the output is:
C:\Users\bob\AppData\Local\Temp\ccuWLUIl.o:test.c:(.text+0xe): undefined reference to `calParameter'
C:\Users\bob\AppData\Local\Temp\ccuWLUIl.o:test.c:(.rdata$.refptr.dSemiMajorAxis[.refptr.dSemiMajorAxis]+0x0): undefined reference to `dSemiMajorAxis'
collect2.exe: error: ld returned 1 exit status
but I want to just include the "Get_para.h" then I can use the implement of them in "Get_para.c".I search this in google , others' code didn't work on my computer. Now I guess the problem is the parameters of gcc, but can't figure out what is it or what knowledge of C I need to know to solve this problem.
You have to compile the implementation of the function too:
gcc Get_para.c test.c -o test
Consider learning a build system, like cmake.
#define _USE_MATH_DEFINES
For a macro like this to work, it has to be defined before any includes.
#define _USE_MATH_DEFINES
#include <math.h>
#define _GETPARA_H_
It's not valid to define your macros with leading _ followed by an upper case letter. Such identifiers are reserved. For example, use:
#define GETPARA_H_
I'm trying to make my school assignment to work in both GCC and MS VS enviroments, but for some reason, it fails to compile in MS VS...
The errors are:
warning C4627: '#include ': skipped when looking for precompiled header use - Add directive to 'Stdafx.h' or rebuild precompiled header
or
unexpected #endif (the one after #include "Stdafx.h")
or
When I put the "Stdafx.h" header to first line, it behaves like there's no stdio and everything (HANDLE, int, etc. ) is illegal declaration.
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include "Stdafx.h"
#include <windows.h>
#endif // _MSC_VER
#ifdef __GNUC__
#include <unistd.h>
#endif // __GNUC__
#ifdef _MSC_VER
int main ()
{
printf("___MS VS Studio/Express compiler___\n");
/*some stuff here*/
return 0;
}
#endif //_MSC_VER
#ifdef __GNUC__
int main()
{
printf("___GCC compiler___\n");
/*some other stuff here*/
return 0;
}
#endif // __GNUC__
It works fine on GCC and I suspect it has something to do with the #includes in #ifdef conditions in MS VS, but I dunno how to do it correctly..
Can anyone please correct me on how to make this work properly?
Any useful advice welcome, thanks!
Your file should start like below, the "stdafx.h" file must be included first. This is required by Visual Studio's "precompiled headers" feature.
#ifdef _MSC_VER
#include "Stdafx.h"
#include <windows.h>
#endif // _MSC_VER
#include <stdio.h>
#include <stdlib.h>
#ifdef __GNUC__
#include <unistd.h>
#endif // __GNUC__
If this doesn't workmtry to do launch the Build - Rebuild Solution command.
If this doesn't work you can remove the precompiled header like this:
Launch the Project - Properties command
In the C/C++ - Precompoiled Headers tab click on "Precompiled Headers" and select "Not Using Precompiled Headers".
I have two source files, main.c and datamgr.c - and two header files, config.h and datamgr.h
The testing system we're using expects these files, and only these files.
main.c:
#include "datamgr.h"
#include "config.h"
int main() {
custom_type a = 1;
a = foo();
return 0;
}
datamgr.c:
#include "datamgr.h"
#include "config.h"
custom_type foo() {
custom_type a = 1;
return a;
}
datamgr.h:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.h:
#ifndef CONFIG_H
#define CONFIG_H
#ifndef SET_MAX_TEMP
#error "Max temperature not set."
#endif
#ifndef SET_MIN_TEMP
#error "Max temperature not set."
#endif
typedef custom_type uint16_t
#endif
Now, the problem is that I can only define SET_MAX_TEMP and SET_MIN_TEMP in main.c, but both main.c and datamgr.c need both the header files. So if I leave them undefined in datamgr.c I get a compiler error. However, if I do define them in datamgr.c and later overwrite them in main.c, I get a different compiler error.
Please, any assistance as to how to get this horrible setup to work would be greatly appreciated.
You can pass these defines directly while compiling:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
In datamgr.c do:
#define SET_MAX_TEMP
#define SET_MIN_TEMP
#include "datamgr.h"
#include "config.h"
#undef SET_MAX_TEMP
#undef SET_MIN_TEMP
In a comment, you said:
Because main.c is the file that our testing system uses to implement the test scenarios.
In that case, make sure that the testing system defines those macros in the command line of the compiler for every file being compiled.
I have my main C file:
#if defined(WIN32)
#include <windows.h>
#include <stddef.h>
#endif
#if defined(LINUX)
#include <curses.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(WIN32)
#include <conio.h>
#endif
#include <ctype.h>
#include <a429usbnt.h>
#if defined(WIN32)
#include "genlib.h"
#endif
void main()
{
_open_xpc(1);
}
When I try to compile using this command
gcc -I. -L. test.c -o test
I get the following error: undefined reference to '_open_xpc'.
However if I change the call to the _open_xpc function and instead just
printf("%d", XPC_ERROR_ACTIONCODE);
the program compiles fine and the correct value assigned to the definition of XPC_ERROR_ACTIONCODE is printed out, so the compiler is linking a429usbnt.h but will only recognize defined variables and not the functions.
If you are trying to link against a .lib file with gcc, it seems you need to define a directory with -L and an actual file with -l
I have a C-Project, which I would like to boost using a CUDA-module. But somehow, the externally defined variables can not be resolved. I am using Microsoft Visual C++ 2010 Express and CUDA Toolkit 5.0.
The following shows my minimal (not) working example:
main.c:
#include "main.h"
#include <stdio.h>
#include "cuda_test.cu"
int main( int argc, const char* argv[] )
{
testfunc();
return 1;
}
main.h:
#ifndef main_h
#define main_h
extern float PI;
#endif
testfile.c:
#include "main.h"
float PI = 3;
cuda_test.cu:
#include "main.h"
#include <stdio.h>
void testfunc()
{
printf("Hello from cudafile: %E", PI);
}
This yields the following error:
1>------ Build started: Project: cuda_min, Configuration: Debug Win32 ------
1>cuda_test.cu.obj : error LNK2001: unresolved external symbol "float PI" (?PI##3MA)
1>D:\Backup\diplomarbeit\cuda_xanthos\cuda_min\Debug\cuda_min.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
When passing the variable PI to the function testfunc, I get the desired behavior. This is what I am doing in my project (which actually uses the CUDA-device), but I really do not want to pass about 20 variables to my functions.
I suppose I am missing some setting for nvcc...
Any help would be much appreciated.
.cu is compiled and linked as .cpp, not .c. So, you can either rename your .c files to .cpp, or use extern "C" in your .cu file.