Linking C++ - BDB - LNK2001 - linker

BDB is compiled on the same PC, under same VS.
#include <db_cxx.h>
int main(){
Db b(NULL, 0);
return 0;
}
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual __thiscall Db::~Db(void)" (??1Db##UAE#XZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: __thiscall Db::Db(class DbEnv *,unsigned int)" (??0Db##QAE#PAVDbEnv##I#Z)"
what could be wrong?

Did you remember to link in the binary created by compiling BDB?

Related

Getting LNK errors trying to compile from .asm file using ml.exe

Here's my code:
#include <stdio.h>
int main(void) {
printf("Hello! \n");
return 0;
}
I call first cl /Fa test.c, then call ml test.asm and I get the errors:
LIBCMT.lib(default_local_stdio_options.obj) : error LNK2005:
___local_stdio_printf_options already defined in test.obj
libvcruntime.lib(undname.obj) : error LNK2005: ___local_stdio_printf_options already defined in test.obj
test.exe : fatal error LNK1169: one or more multiply defined symbols found
It works fine if I don't use printf. What am I doing wrong?

Using CMake to compile the project

My project structure looks like this.
build
- folder where cmake outputs the solution file
client
- client.c
- CMakeLists.txt
server
- server.c
- CMakeLists.txt
open62541
- open62541.c
- open62541.h
- CMakeLists.txt
CMakeLists.txt
The main CMakeLists.txt has the following code
cmake_minimum_required(VERSION 3.0)
project(openstack)
add_subdirectory(open62541)
add_subdirectory(server)
open62541/CMakeLists.txt has the following code
project(Lib1)
add_library(lib1 open62541.c open62541.h)
and server/CMakeLists.txt looks like this
project(App)
add_executable(app server.c)
include_directories(${Lib1_SOURCE_DIR})
target_link_libraries(app PRIVATE lib1 ws2_32 wsock32)
server.c has the following code.
#include "open62541.h"
#include "signal.h"
#include <stdlib.h>
#include "stdio.h"
static volatile UA_Boolean running = true;
static void stopHandler(int sig) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "received ctrl-c");
running = false;
}
int main(void) {
signal(SIGINT, stopHandler);
signal(SIGTERM, stopHandler);
UA_Server *server = UA_Server_new();
UA_ServerConfig_setDefault(UA_Server_getConfig(server));
UA_StatusCode retval = UA_Server_run(server, &running);
UA_Server_delete(server);
return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
}
server.c needs functions defined and declared in open62541.c and open62541.h.(I am not building the client project as of now)So, I have included open62541.h in the server.c file and in the server/CMakeLists.txt, I have given the link to the open62541 folder as you can see. I am getting the following linker errors in visual studio. The message is in german sorry for that.
Erstellen gestartet...
1>------ Erstellen gestartet: Projekt: app, Konfiguration: Debug x64 ------
1>server.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp_UA_Server_delete" in Funktion "main".
1>server.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp_UA_Server_getConfig" in Funktion "main".
1>server.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp_UA_Server_run" in Funktion "main".
1>server.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp_UA_Server_new" in Funktion "main".
1>server.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp_UA_ServerConfig_setMinimalCustomBuffer" in Funktion "UA_ServerConfig_setMinimal".
1>server.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol "__imp_UA_Log_Stdout" in Funktion "stopHandler".
1>C:\Users\acer\CMake_Project\build\server\Debug\app.exe : fatal error LNK1120: 6 nicht aufgelöste Externe
1>Die Erstellung des Projekts "app.vcxproj" ist abgeschlossen -- FEHLER.
2>------ Erstellen übersprungen: Projekt: ALL_BUILD, Konfiguration: Debug x64 ------
2>Für diese Projektmappenkonfiguration wurde kein zu erstellendes Projekt ausgewählt.
========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 2 aktuell, 1 übersprungen ==========
The project builds fine when I include server. c,open62541.c,open62541.h files in a single folder and use a single CMakeLists.txt file like this.
cmake_minimum_required(VERSION 3.0)
project(openstack)
set(SOURCES server.c open62541.c open62541.h)
add_executable(openstack ${SOURCES})
target_link_libraries(openstack PRIVATE ws2_32 wsock32)
However, I get errors when I try to structure my project in different folders. How can I solve these errors?
I've restructured your project a little to be more modern. See if this works and tell me what you get.
cmake_minimum_required(VERSION 3.15)
project(openstack LANGUAGES C)
add_subdirectory(open62541)
add_subdirectory(server)
# You didn't add_subdirectory client.
add_subdirectory(client)
open62541/CMakeLists.txt has the following code
add_library(lib1 STATIC)
target_sources(lib1 PRIVATE open62541.c open62541.h)
# Prefer to use public. To ease use of your library.
target_include_directores(lib1 PUBLIC ${CMAKE_CURRENT_LIST_DIR)
and server/CMakeLists.txt looks like this
add_executable(app)
target_sources(app PRIVATE server.c)
target_link_libraries(app PRIVATE lib1 ws2_32 wsock32)

running a c project in visual studio 2015 I have got Error LNK1120 3 unresolved externals

I am running a C project in Visual Studio 2015 I have got these errors:
Severity Code Description Project File Line
Error LNK1120 3 unresolved externals f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\Debug\f90toC_B.exe 1
Error LNK2019 unresolved external symbol _yyparse referenced in function _main f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\f90main.obj 1
Error LNK2019 unresolved external symbol _finclude referenced in function _getMfile f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\f90mod.obj 1
Error LNK2019 unresolved external symbol _strcasecmp referenced in function _findarg f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\ptree.obj 1
Error LNK2001 unresolved external symbol _strcasecmp f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\symb.obj 1
My files tree:
C:.
│ f90.h
│ f90.lex
│ f90.y
│ f90arg.c
│ f90arg.h
│ f90arg.o
│ f90decl.c
│ f90decl.h
│ f90decl.o
│ f90main.c
│ f90main.h
│ f90main.o
│ f90mod.c
│ f90mod.h
│ f90print.c
│ File_index
│ Makefile
│ ptree.c
│ ptree.h
│ README
│ symb.c
│ symb.h
│
├───doc
│ description.txt
│
└───tests
Makefile
mtest1.f90
mtest1.M.std
mtest1.std
test1.f90
test1.std
test2.f90
test2.std
test3.f90
test3.std
First error and section of code:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol _yyparse referenced in function _main f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\f90main.obj 1
code:
f90main.c
#include "f90.h"
#include "ptree.h"
#include "symb.h"
#include "f90main.h"
#include "f90decl.h"
#include "f90arg.h"
main()
{
yyparse();
}
int lineno=1;
extern struct declist *declisthead;
extern int in_module;
Second error and section of code:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol _finclude referenced in function _getMfile f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\f90mod.obj 1
f90mod.c
/* handles things related to modules */
#include "symb.h"
#include "ptree.h"
#include "f90mod.h"
#include <stdio.h>
struct declist *modlist=NULL;
int doing_module=0;
int in_module=0;
int in_user_type=0;
char *curr_modname=NULL;
struct declist *modsubshead=NULL;
void getMfile(char *nam)
{
char name[35];
modlist=newdeclist(TBLANK,nam,modlist,NULL);
curr_modname=modlist->name;
doing_module=1;
strcpy(name,nam);
strcpy(name+strlen(name),".M");
/*fprintf(stderr,"mod name: %s\n",name);*/
finclude(name);
}
Third error and section of code:
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol _strcasecmp referenced in function _findarg f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\ptree.obj 1
ptree.c
/* finds a keyword based argument with name "name" */
struct tree *findarg(struct tree *t, char *name)
{
struct tree *t1;
if (t==NULL) return(NULL);
if (t->ntype==ALISTNODE) {
if ( (t1=findarg(t->child1,name)) != NULL) return(t1);
if (t->child2->ntype == ARGKWNODE) {
if (strcasecmp(t->child2->child1->num.s,name)==0) {
return (t);
} else {
return(NULL);
}
}
} else {
fprintf(stderr,"Do I ever get here in findarg?\n");
}
return(NULL);
}
Fourth error and section of code:
Severity Code Description Project File Line
Error LNK2001 unresolved external symbol _strcasecmp f90toC_B C:\Users\admin\Documents\Visual Studio 2015\Projects\f90toC_B\f90toC_B\symb.obj 1
symb.c
int findsymbt(struct declist *d,char *s)
{
int f;
if (d==NULL) return(0);
/*printf("symb: ntype %d name %s\n",d->ntype,d->name);*/
if (d->ntype==TSEQ) {
if ( (f=findsymbt(d->next,s))!=0 ) {return(f);}
f=findsymbt(d->next2,s);
return(f);
} else {
if ((f=strcasecmp(d->name,s))==0) {return (d->ntype);}
else {return (0);}
}
printf("I shouldn't be here \n");
}
So what should I do ?
My Makefile
CC = cc -ggdb
OBJ = f90main.o f90decl.o f90arg.o f90.tab.o lex.yy.o symb.o ptree.o \
f90mod.o f90print.o
.c.o:
$(CC) -c $<
all: f90
lex.yy.c: f90.lex
flex -i f90.lex
f90.tab.c: f90.y f90.h
bison -d f90.y
f90: $(OBJ) f90.h
$(CC) -o ff90 $(OBJ)
why:
bison -d -v f90.y
clean:
-rm *.o lex.yy.c f90.tab.c f90.tab.h f90.output
F = f90toC
tar:
tar -cvf f90toC-test.tar $(F)/f90.lex $(F)/f90.y $(F)/*.h $(F)/*.c $(F)/tests $(F)/doc $(F)/Makefile $(F)/README $(F)/File_index
bak:
cp *.c backup
cp *.h backup
cp *.y backup
cp *.lex backup
cp Makefile backup
edited 2 add a part of f90.lex
...
finclude(char *text){
if (include_stack_ptr >= MAX_INCLUDE_BUFF) {
fprintf(stderr,"Includes nested too deep\n");
exit(1);
}
lineno_stack[include_stack_ptr] = lineno; lineno=1;
include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
yyin = fopen(text,"r");
if (!yyin) {fprintf(stderr,"Failed to include:%s\n",text);
include_stack_ptr--;
return;}
yy_switch_to_buffer(yy_create_buffer(yyin,YY_BUF_SIZE));
}
edited 3 add f90.y
edited 4 deleted f90.yas not related to the discution
The errors are not in the code that you are showing us. The errors are either in the header files that you are including, (functions declared as something other than _cdecl, so their names do not begin with underscores,) or in your makefile / project file (you are simply not linking the object files generated from the .c files that declare the functions that you are invoking.)
My problem solved as I follow instruction in How to compile LEX/YACC files on Windows?
and use this project http://www.ncsa.illinois.edu/People/mdewing/f90toC/
and make the project in command prompt.Not Visual Studio 2015 environment
so I use ff90.exe to compile fortran .f90 files to .c files as the instruction mentioned .but the ff90.exe is not really strong and take errors in such tests.this is related to the developer.

undefined symbol for architecture x86_64 in compiling C program [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
I have a simple program as
demo_use.c
#include "libhello.h"
int main(void) {
hello();
return 0;
}
libhello.h
void hello(void);
libhello.c
#include <stdio.h>
void hello(void) {
printf("Hello, library world.\n");
}
I have used the command in terminal as
gcc demo_use.c -o test
error
Undefined symbols for architecture x86_64: "_hello",
referenced from: _main in ccZdSQP3.o
ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
You need to compile both the source files together to generate the binary. use
gcc demo_use.c libhello.c -o test
Otherwise, the definition of hello() function will be absent. So, at linking time, linker will throw undefined symbol error.

Taking a screenshot C++ cli

I've seen the following code that takes a screenshot and saves it as jpg, I've managed to compile and run it as win32 CONSOLE application,
But when I tried to use the following code in A windowsForm/CLI (there is just a button that should take a screenshot) project, I got the following errors:
1>screenshoter.obj : warning LNK4248: unresolved typeref token (0100002C) for 'Gdiplus.GpCachedBitmap'; image may not run
1>screenshoter.obj : error LNK2028: unresolved token (0A000017) "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC##$$J18YGHPAUHWND__##PAUHDC__###Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A000018) "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject##$$J14YGHPAX#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A000019) "extern "C" int __stdcall BitBlt(struct HDC__ *,int,int,int,int,struct HDC__ *,int,int,unsigned long)" (?BitBlt##$$J236YGHPAUHDC__##HHHH0HHK#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001A) "extern "C" void * __stdcall SelectObject(struct HDC__ *,void *)" (?SelectObject##$$J18YGPAXPAUHDC__##PAX#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001B) "extern "C" struct HBITMAP__ * __stdcall CreateCompatibleBitmap(struct HDC__ *,int,int)" (?CreateCompatibleBitmap##$$J212YGPAUHBITMAP__##PAUHDC__##HH#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001C) "extern "C" struct HDC__ * __stdcall CreateCompatibleDC(struct HDC__ *)" (?CreateCompatibleDC##$$J14YGPAUHDC__##PAU1##Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001D) "extern "C" int __stdcall GetSystemMetrics(int)" (?GetSystemMetrics##$$J14YGHH#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2028: unresolved token (0A00001E) "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC##$$J14YGPAUHDC__##PAUHWND__###Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall ReleaseDC(struct HWND__ *,struct HDC__ *)" (?ReleaseDC##$$J18YGHPAUHWND__##PAUHDC__###Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall DeleteObject(void *)" (?DeleteObject##$$J14YGHPAX#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall BitBlt(struct HDC__ *,int,int,int,int,struct HDC__ *,int,int,unsigned long)" (?BitBlt##$$J236YGHPAUHDC__##HHHH0HHK#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" void * __stdcall SelectObject(struct HDC__ *,void *)" (?SelectObject##$$J18YGPAXPAUHDC__##PAX#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" struct HBITMAP__ * __stdcall CreateCompatibleBitmap(struct HDC__ *,int,int)" (?CreateCompatibleBitmap##$$J212YGPAUHBITMAP__##PAUHDC__##HH#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" struct HDC__ * __stdcall CreateCompatibleDC(struct HDC__ *)" (?CreateCompatibleDC##$$J14YGPAUHDC__##PAU1##Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall GetSystemMetrics(int)" (?GetSystemMetrics##$$J14YGHH#Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
1>screenshoter.obj : error LNK2019: unresolved external symbol "extern "C" struct HDC__ * __stdcall GetDC(struct HWND__ *)" (?GetDC##$$J14YGPAUHDC__##PAUHWND__###Z) referenced in function "private: void __clrcall screenshoter::Form1::gdiscreen(void)" (?gdiscreen#Form1#screenshoter##$$FA$AAMXXZ)
Note: That I've included all the files needed
I looked up in MSDN, but I couldn't really understand the error messeges. Moreover, I had encountered similar messages in the past, i solved it but adding a .lib file
This time I tried adding, however it didn't solve the problem
#pragma comment( lib, "GdiPlus.lib" )
Those functions are declared in gdi32.lib - not gdiplus.lib - and some might be in user32.lib.
Those error messages mean that your code (or rather, the 'screenshooter' code) used 'symbols' (could be function names or variables, for instance) for which there is no code linked into the final binary.
To fix this, look up what libraries you need to link against for each of those symbols. For instance, GetDC appears to be defined in user32.lib, so make sure to link against that (possibly by using a #pragma directive as you did for GdiPlus.lib).

Resources