I have a problem with this project (it is a minigame similar to space invaders).
Every time I try to compile it it gives me these errors:
❯ make
gcc main.o collisioni.o stampa.o gestione.o -o progetto -lncurses
/usr/bin/ld: collisioni.o:(.bss+0x0): multiple definition of `maxx'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: collisioni.o:(.bss+0x4): multiple definition of `maxy'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: collisioni.o:(.bss+0x8): multiple definition of `flag_navicelle'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: stampa.o:(.bss+0x0): multiple definition of `maxx'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: stampa.o:(.bss+0x4): multiple definition of `maxy'; main.o:(.bss+0x4): first defined here
/usr/bin/ld: stampa.o:(.bss+0x8): multiple definition of `flag_navicelle'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: gestione.o:(.bss+0x0): multiple definition of `maxx'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: gestione.o:(.bss+0x4): multiple definition of `maxy'; main.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status
make: *** [makefile:2: progetto] Errore 1
The source code of the project is this: https://github.com/Stenafo/game
I compile the code using the makefile.
I don't understand where the problem is, a friend of mine manages to compile it without errors.
This project uses processes, however I have the same problem with threads.
Related
here is the error when i going to make psim.
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x0): multiple definition of `mem_wb_state'; /tmp/ccQX7AGh.o:(.bss+0x120): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x8): multiple definition of `ex_mem_state'; /tmp/ccQX7AGh.o:(.bss+0x128): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x10): multiple definition of `id_ex_state'; /tmp/ccQX7AGh.o:(.bss+0x130): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x18): multiple definition of `if_id_state'; /tmp/ccQX7AGh.o:(.bss+0x138): first defined here
/usr/bin/ld: /tmp/ccZiibmg.o:(.bss+0x20): multiple definition of `pc_state'; /tmp/ccQX7AGh.o:(.bss+0x140): first defined here
This is the makefile which is under the directory 'pipe'
VERSION=full
GUIMODE=-DHAS_GUI
TKLIBS=-L/usr/lib -ltk -ltcl
TKINC=-isystem /usr/include/tcl8.6
CC=gcc
CFLAGS=-Wall -O2 -DUSE_INTERP_RESULT
How to fix it?
I try to compile/build hackrf.c since i'm using the library in another code, I have hackrf.h in the same directory as the one i'm building hackrf.c in...
https://github.com/greatscottgadgets/hackrf/tree/master/host/libhackrf/src
Ideally, it should do so, but instead I get these errors in the terminal:
choza#chozaUbuntu:~/Documents/workspace_c$ gcc hackrf.c
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `cancel_transfers':
hackrf.c:(.text+0x139): undefined reference to `libusb_cancel_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `free_transfers':
hackrf.c:(.text+0x25b): undefined reference to `libusb_free_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `allocate_transfers':
hackrf.c:(.text+0x337): undefined reference to `libusb_alloc_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `prepare_transfers':
hackrf.c:(.text+0x484): undefined reference to `libusb_submit_transfer'
/usr/bin/ld: /tmp/ccrRkrH1.o: in function `detach_kernel_drivers':
hackrf.c:(.text+0x4ee): undefined reference to `libusb_get_device'
/usr/bin/ld: hackrf.c:(.text+0x505): undefined reference to `libusb_get_active_config_descriptor'
/usr/bin/ld: hackrf.c:(.text+0x53b): undefined reference to `libusb_free_config_descriptor'
/usr/bin/ld: hackrf.c:(.text+0x555): undefined reference to `libusb_kernel_driver_active'
/usr/bin/ld: hackrf.c:(.text+0x592): undefined reference to `libusb_detach_kernel_driver'
And so on...
Then I suppose I should directly link libusb...
But I get this:
choza#chozaUbuntu:~/Documents/workspace_c$ gcc hackrf.c -lusb-1.0
/usr/bin/ld: /tmp/ccfIzbqD.o: undefined reference to symbol 'pthread_join##GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
And pthread...
choza#chozaUbuntu:~/Documents/workspace_c$ gcc hackrf.c -lusb-1.0 -pthread
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
I really don't know how to fix the error above or how to successfully compile the .c file anymore.
I'm using VS Code in Ubuntu 20.04.
What should I do?
I really don't know how to fix the error above or how to successfully compile the .c file anymore.
You aren't having a problem compiling, you are having a problem linking.
The reason your link fails is that hackrf.c lacks main() function, which every C program requires. And that's because it's part of a library.
Generally you'll want to build the entire project, not just a library, using the instructions the project provides.
If you do want to build a library, then you should link it with the rest of your program.
I have just seen this post: How to tell where the symbols are defined when linking C code ; and tried to run that approach (that is, I tried to look up via -Wl,-trace-symbol=foo during the linking command) on my code, which suffers from undefined reference - but the output is not much different from what is given as an example in that post:
/usr/bin/ld: main.o: reference to foo
/usr/bin/ld: ./libfoo.a(foo.o): definition of foo
/usr/bin/ld: ./libfoo.a(foo.o): reference to fputs
/usr/bin/ld: //lib/x86_64-linux-gnu/libc.so.6: definition of fputs
The thing is, I've manually added both -L and -l specifications to my command line, which I expect should pass - but they do not.
So what I am ultimately looking for, is a way to generate a log, that tells me, say:
/usr/bin/ld: main.o: reference to foo
/usr/bin/ld: ./libyoo.a(yooA.o): symbol foo looked up, not found
/usr/bin/ld: ./libyoo.a(yooB.o): symbol foo looked up, not found
/usr/bin/ld: ./libfoo.a(foo.o): definition of foo
... which, I hope, will make me understand a bit easier, which include and linker flag directories and files end up being scanned.
Is there a way to generate a log like this?
I am using a Raspberry Pi to run a C program that interacts with a can interface, and stores values in a database as part of a project.
I started C one month ago, and I am still a bit lost with everything.
Description of my workspace and problem :
I have my test file test virtual.c that includes a homemade file handler_code.c that includes the sqlite3.c amalgamation file(via #include "sqlite3.c").
Then I have a CMakeLists.txt that was provided to me :
if( CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR )
set( STANDALONE TRUE )
cmake_minimum_required(VERSION 3.13)
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
find_package(CARLOS REQUIRED)
else()
include_directories(${CMAKE_SOURCE_DIR}/core/include)
include_directories(${CMAKE_BINARY_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/gui/include)
endif()
add_definitions(-DRESOURCES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/resources")
add_executable(test_example "test virtual.c")
target_link_libraries(test_example carlos)
set_target_properties(
test_example
PROPERTIES
C_STANDARD 99
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/examples_bin
)
It imports all libraries I need. All the files are in the same directory.
I then go in the subdirectory build and run cmake .. which succeeds and then make. This is what happens :
pi#raspberrypi:~/Desktop/examples/build $ make
[ 50%] Building C object CMakeFiles/test_example.dir/test_virtual.c.o
In file included from /home/pi/Desktop/examples/handler_code.c:5,
from /home/pi/Desktop/examples/test virtual.c:8:
/home/pi/Desktop/examples/sqlite3.c:34058:42: error: ‘mremap’ undeclared here (not in a function); did you mean ‘munmap’?
{ "mremap", (sqlite3_syscall_ptr)mremap, 0 },
^~~~~~
munmap
/home/pi/Desktop/examples/sqlite3.c: In function ‘unixRemapfile’:
/home/pi/Desktop/examples/sqlite3.c:38625:42: error: ‘MREMAP_MAYMOVE’ undeclared (first use in this function)
pNew = osMremap(pOrig, nReuse, nNew, MREMAP_MAYMOVE);
^~~~~~~~~~~~~~
/home/pi/Desktop/examples/sqlite3.c:38625:42: note: each undeclared identifier is reported only once for each function it appears in
/home/pi/Desktop/examples/sqlite3.c:38625:42: warning: passing argument 4 of ‘(void * (*)(void *, size_t, size_t, int, ...))aSyscall[24].pCurrent’ makes integer from pointer without a cast [-Wint-conversion]
/home/pi/Desktop/examples/sqlite3.c:38625:42: note: expected ‘int’ but argument is of type ‘struct unix_syscall *’
make[2]: *** [CMakeFiles/test_example.dir/build.make:63: CMakeFiles/test_example.dir/test_virtual.c.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/test_example.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I tried only compiling sqlite3.c with gcc and I got this :
pi#raspberrypi:~/Desktop/examples $ gcc sqlite3.c
/usr/bin/ld: /usr/lib/gcc/arm-linux-gnueabihf/8/../../../arm-linux-gnueabihf/crt1.o: in function `_start':
(.text+0x34): undefined reference to `main'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `pthreadMutexAlloc':
sqlite3.c:(.text+0x5098): undefined reference to `pthread_mutexattr_init'
/usr/bin/ld: sqlite3.c:(.text+0x50a8): undefined reference to `pthread_mutexattr_settype'
/usr/bin/ld: sqlite3.c:(.text+0x50c8): undefined reference to `pthread_mutexattr_destroy'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `pthreadMutexTry':
sqlite3.c:(.text+0x51b8): undefined reference to `pthread_mutex_trylock'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `sqlite3ThreadCreate':
sqlite3.c:(.text+0x9fcc): undefined reference to `pthread_create'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `sqlite3ThreadJoin':
sqlite3.c:(.text+0xa084): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlOpen':
sqlite3.c:(.text+0x148c0): undefined reference to `dlopen'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlError':
sqlite3.c:(.text+0x148f4): undefined reference to `dlerror'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlSym':
sqlite3.c:(.text+0x14970): undefined reference to `dlsym'
/usr/bin/ld: /tmp/cc6Quw7S.o: in function `unixDlClose':
sqlite3.c:(.text+0x1498c): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
I also did some tests on my windows computer. The file named config.c includes the amalgamation just like handler_code.c did.
PS C:\Users\p107770\Desktop\projects\hmi-rapid-prototyping\C> gcc sqlite3.c
C:/MATLAB/V_R2016b/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.2/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): In function `main':
C:/crossdev/src/mingw-w64-v3-git/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
PS C:\Users\p107770\Desktop\projects\hmi-rapid-prototyping\C> gcc config.c
PS C:\Users\p107770\Desktop\projects\hmi-rapid-prototyping\C>
So here, it successfully compiles the file that includes the amalgamation file (which is what I'd want on my raspberry pi, even though it's not the same file), but it does not compile the amalgamation file alone (which I don't understand).
Ideas I found while searching
Using sqlite as a library/package in CMake file
Something looking like this. But looking at my CMake file, I realized that my sqlite amalgamation file was not a "package" in the same way CARLOS and Threads are (I installed CARLOS with .deb files provided to me). So my question here is how to transform the amalgamtion file into a package ?
Compiling sqlite so it would be imported globally
I remember reading something about this. If I recall correctly, I can compile the sqlite amalgamation file into a .dll and make it available in any C file with the #include <sqlite3.c> line instead of #include "sqlite3.c", but I don't know if it would work here because I think this was working when compiling with gcc and not with make.
Thank you in advance for any help you could bring, I can add bits of code if needed.
Edit : initializing the constants of my project in a C module and declaring them in a corresponding header resolved the issue.
I am working on making a C implementation of the AES cipher (link : https://github.com/SuperTotoGo/AES_Cipher). Everything was working fine until I tried to separate my project in modules. Now when I compile I get this error :
gcc -std=c99 -g -o aes.out *.c
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/ccpeYNuO.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple definition of `RCON'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:104: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/cczfD784.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple definition of `RCON'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:104: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:8: multiple definition of `AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:8: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:28: multiple definition of `INV_AES_SUB_BOX'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:28: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:48: multiple definition of `AES_LOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:48: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:68: multiple definition of `AES_ALOG_TABLE'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:68: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:88: multiple definition of `AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:88: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:96: multiple definition of `INV_AES_MULT_MAT'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:96: first defined here
/usr/bin/ld: /tmp/ccxcDlRl.o:/home/pipou/AES_Cipher/aes_const.h:104: multiple definition of `RCON'; /tmp/ccHvhAUx.o:/home/pipou/AES_Cipher/aes_const.h:104: first defined here
collect2: error: ld returned 1 exit status
My functions prototypes are defined in the headers and my functions are implemented in the corresponding c files, from what I understood about modular projects I normally only need to include the header files in the main file to have it working.
So, would you have any idea what I did wrong?
Also if you have any tips about making modular C projects or any advice conecerning how I have done it, please tell me!
You have several variables defined in the aes_const.h header file. As a result, each source file that is compiled has a copy of those variables. So when everything in linked, you end up with multiple definitions.
Move these variable definitions into a single source file, then put extern declarations for them in the header file.
So the header should contain:
extern const uint8_t AES_SUB_BOX[16][16];
extern const uint8_t INV_AES_SUB_BOX[16][16];
extern const uint8_t AES_LOG_TABLE[16][16];
extern const uint8_t AES_ALOG_TABLE[16][16];
extern const uint8_t AES_MULT_MAT[4][4];
extern const uint8_t INV_AES_MULT_MAT[4][4];
extern uint8_t RCON[10];
And the existing definitions should be moved to a .c file, for example aes_const.c.
The prototypes are defined in the headers, which is why the compiler detects all the names and succeeds in compilation. The failure you're getting is during link-time. That is because the linker needs the implementation of the functions, which live in the .c files.
What you need to do is to compile all the .c files and link the resulting object files together. C projects usually build all the object files first (gcc -c *.c) and then link them all together in a separate step (gcc -o aes.out *.o)