Using CMake to compile the project - c

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)

Related

trying to compile a c program but I am getting fatal error:/usr/include/rpc/misc/abstract_atomic.h

I am trying to compile my programm. First I got the fatal error:rpc/rpc.h, tried some fixes and then fatal error:/usr/include/rpc/misc/abstract_atomic.h. Now I get fatal error: netconfig.h. I have no idea what to do. Can someone help?
┌──(kali㉿kali)-[~/Test/Blatt4]
└─$ gcc -c rls_clnt.c rls_svc.c rls_xdr.c
In file included from /usr/include/rpc/rpc.h:38,
from rls.h:9,
from rls_clnt.c:7:
/usr/include/rpc/types.h:240:10: fatal error: netconfig.h: Datei oder Verzeichnis nicht gefunden
240 | #include <netconfig.h>
| ^~~~~~~~~~~~~
compilation terminated.
In file included from /usr/include/rpc/rpc.h:38,
from rls.h:9,
from rls_svc.c:6:
/usr/include/rpc/types.h:240:10: fatal error: netconfig.h: Datei oder Verzeichnis nicht gefunden
240 | #include <netconfig.h>
| ^~~~~~~~~~~~~
compilation terminated.
In file included from /usr/include/rpc/rpc.h:38,
from rls.h:9,
from rls_xdr.c:6:
/usr/include/rpc/types.h:240:10: fatal error: netconfig.h: Datei oder Verzeichnis nicht gefunden
240 | #include <netconfig.h>
| ^~~~~~~~~~~~~
compilation terminated.

Undefined reference to "graphics.h" functions in Ubuntu 19.04 in CLion IDE

I installed graphics.h library on my Ubuntu 19.04, and when I write some code in my CLion IDE, it successfully includes the graphics.h Header file, and also after writing some basic functions, the IDE doesn't show any errors, but when I compile the Code it gives Errors.
My Code:
#include <stdio.h>
#include <graphics.h>
int main() {
int gd = DETECT, gm;
detectgraph(&gd, &gm);
initgraph(&gd, &gm, NULL);
line(10, 10, 50, 50);
closegraph();
}
My CMakeList.txt File:
cmake_minimum_required(VERSION 3.14)
project(CGR_MP C)
set(CMAKE_C_STANDARD 11)
include_directories("//usr//local//lib//libgraph-1.0.2//")
build_command("lgraph")
add_executable(CGR_MP main.c)
Errors:
====================[ Build | CGR_MP | Debug ]==================================
/snap/clion/73/bin/cmake/linux/bin/cmake --build /home/prathamesh/Desktop/College/Sem-2/CGR-MP/cmake-build-debug --target CGR_MP -- -j 2
Scanning dependencies of target CGR_MP
[ 50%] Building C object CMakeFiles/CGR_MP.dir/main.c.o
[100%] Linking C executable CGR_MP
/usr/bin/ld: CMakeFiles/CGR_MP.dir/main.c.o: in function `main':
/home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:7: undefined reference to `detectgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:8: undefined reference to `initgraph'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:10: undefined reference to `line'
/usr/bin/ld: /home/prathamesh/Desktop/College/Sem-2/CGR-MP/main.c:12: undefined reference to `closegraph'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/CGR_MP.dir/build.make:84: CGR_MP] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/CGR_MP.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/CGR_MP.dir/rule] Error 2
make: *** [Makefile:118: CGR_MP] Error 2
Despite everything being successfully installed, it shows all Graphics functions as undefined reference

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.

use extern structure variable defined in a library, config with cmake

main.c
#include "test.h"
extern struct test_struct my_test_str; // defined in my_test_lib.a
int main(int argc, char *argv[]) {
return 0;
}
here is the my_test_my library file of test.h and test.c
compiled as my_test_lib.a
test.h
#ifndef TEST_MY
#define TEST_MY
struct test_struct {
double d1;
double d2;
double d3;
};
void ddd(void);
#endif
test.c
struct test_struct my_test_str;
void ddd(void) {
int a = 0;
int c = 1;
c = a + c;
}
write CMakeLists.txt , use cmake to generated Makefile
cmake_minimum_required(VERSION 2.6)
project(Tutorial)
add_library(my_test_my test.c)
add_executable(Tutorial main.c)
target_link_libraries (Tutorial my_test_my)
after generate Makefiles in a out directory, execute the make
192:out jerryw$ make
[ 25%] Building C object CMakeFiles/my_test_my.dir/test.c.o
[ 50%] Linking C static library libmy_test_my.a
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: libmy_test_my.a(test.c.o) has no symbols
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: file: libmy_test_my.a(test.c.o) has no symbols
warning: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ranlib: warning for library: libmy_test_my.a the table of contents is empty (no object file members in the library define global symbols)
[ 50%] Built target my_test_my
[ 75%] Building C object CMakeFiles/Tutorial.dir/main.c.o
[100%] Linking C executable Tutorial
Undefined symbols for architecture x86_64:
"_ddd", referenced from:
_main in main.c.o
"_my_test_str", referenced from:
_main in main.c.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Tutorial] Error 1
make[1]: *** [CMakeFiles/Tutorial.dir/all] Error 2
make: *** [all] Error 2
why this happened ?

Linking C++ - BDB - LNK2001

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?

Resources