Thread: Linkage issue/s

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Linkage issue/s

    So I finally fixed a bunch of includes that needed to be changed as a result of reorganisation and a number of code faults generated as a result of an API change, got everything to compile without issue only to find and the last stretch the linker can't connect the dots, someone tell me what I'm doing wrong here:
    Code:
    INC_PATHS=../include
    IS_WINDOWS:=$(if $(Program Files),1,)
    IS_WIN64:=$(if ${Program Files(x86)},1,)
    IS_WIN32:=$(if $(IS_WIN64),,$(IS_WINDOWS))
    EXE?=$(if $(IS_WINDOWS),exe,AppImage)
    IFLAGS:=$(INC_PATHS:%=-I %)
    CFLAGS:=-Wall $(IFLAGS)
    
    mcc: libmcc_core.so
    test: test.$(EXE)
    
    libmcc_core.so: $(wildcard mcc/core/*.o)
    	gcc -lpthread -o $@ $^
    
    test.AppImage: test/unit_tests.o libmcc_core.so
    	gcc -o $@ $^
    
    mcc/core/vflt.c: mcc/core/vflt.h
    mcc/core/vint.c: mcc/core/vint.h
    mcc/core/get.c: mcc/core/get.h
    mcc/core/mem.c: mcc/core/mem.h
    mcc/core/vflt.h: mcc/core/vint.h
    mcc/core/vint.h: mcc/core/get.h
    mcc/core/get.h: mcc/core/mem.h
    mcc/core/mem.h: mcc/malloc.h
    mcc/malloc.h: mcc/vmalloc.h
    mcc/vmalloc.h: mcc/inttypes.h
    mcc/inttypes.h: mcc/stdint.h
    mcc/stdint.h: mcc/limits.h
    mcc/limits.h: mcc/core/sys.h
    mcc/core/sys.h:
    test/unit_tests.o: test/unit_tests.c
    test/unit_tests.c: test/core/vint.c
    
    %.o: %.c
    	clang $(CFLAGS) -o $@ -c $<
    
    force: ;
    Output from my attempt:
    Code:
    ...
    make[2]: Entering directory '~/mitsy/src'
    clang -Wall -I ../include -o test/unit_tests.o -c test/unit_tests.c
    gcc -lpthread -o libmcc_core.so mcc/core/vint.o mcc/core/vflt.o mcc/core/get.o mcc/core/mem.o
    gcc -o test.AppImage test/unit_tests.o libmcc_core.so
    /usr/bin/ld: test/unit_tests.o: in function `test__vint':
    unit_tests.c:(.text+0x487): undefined reference to `mcc_vint_wrap'
    /usr/bin/ld: unit_tests.c:(.text+0x4af): undefined reference to `mcc_vint_size_and_fill'
    /usr/bin/ld: unit_tests.c:(.text+0x4e2): undefined reference to `mcc_vint_wrap'
    /usr/bin/ld: unit_tests.c:(.text+0x513): undefined reference to `mcc_vint_size_and_fill'
    /usr/bin/ld: unit_tests.c:(.text+0x546): undefined reference to `mcc_vint_wrap'
    /usr/bin/ld: unit_tests.c:(.text+0x577): undefined reference to `mcc_vint_size_and_fill'
    /usr/bin/ld: unit_tests.c:(.text+0x5f3): undefined reference to `mcc__vint_op_not'
    /usr/bin/ld: unit_tests.c:(.text+0x65b): undefined reference to `mcc__vint_op_aor'
    /usr/bin/ld: unit_tests.c:(.text+0x6c3): undefined reference to `mcc__vint_op_xor'
    /usr/bin/ld: unit_tests.c:(.text+0x72b): undefined reference to `mcc__vint_op_and'
    /usr/bin/ld: unit_tests.c:(.text+0x77b): undefined reference to `mcc__vint_op_inc'
    /usr/bin/ld: unit_tests.c:(.text+0x7e3): undefined reference to `mcc__vint_op_add'
    /usr/bin/ld: unit_tests.c:(.text+0x84c): undefined reference to `mcc__vint_op_mul'
    /usr/bin/ld: unit_tests.c:(.text+0x89c): undefined reference to `mcc__vint_op_dec'
    /usr/bin/ld: unit_tests.c:(.text+0x905): undefined reference to `mcc__vint_op_sub'
    /usr/bin/ld: unit_tests.c:(.text+0x9df): undefined reference to `mcc___vint_op_div'
    /usr/bin/ld: unit_tests.c:(.text+0xa95): undefined reference to `mcc___vint_op_div'
    /usr/bin/ld: unit_tests.c:(.text+0xb52): undefined reference to `mcc__vint_op_cmp'
    /usr/bin/ld: unit_tests.c:(.text+0xb9d): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0xc22): undefined reference to `mcc__vint_op_cmp'
    /usr/bin/ld: unit_tests.c:(.text+0xc6d): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0xcf1): undefined reference to `mcc__vint_op_cmp'
    /usr/bin/ld: unit_tests.c:(.text+0xd3c): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0xdd6): undefined reference to `mcc__vint_op_shr'
    /usr/bin/ld: unit_tests.c:(.text+0xe5a): undefined reference to `mcc__vint_op_cmp'
    /usr/bin/ld: unit_tests.c:(.text+0xea5): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0xf15): undefined reference to `mcc__vint_op_cmp'
    make[2]: Leaving directory '~/mitsy/src'
    make[1]: Leaving directory '~/mitsy/src/test'
    /usr/bin/ld: unit_tests.c:(.text+0xf60): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0xfe2): undefined reference to `mcc__vint_op_shl'
    /usr/bin/ld: unit_tests.c:(.text+0x1066): undefined reference to `mcc__vint_op_cmp'
    /usr/bin/ld: unit_tests.c:(.text+0x10b1): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0x1121): undefined reference to `mcc__vint_op_cmp'
    /usr/bin/ld: unit_tests.c:(.text+0x116c): undefined reference to `mcc_vint_to_val'
    /usr/bin/ld: unit_tests.c:(.text+0x1218): undefined reference to `mcc_vint_size'
    /usr/bin/ld: unit_tests.c:(.text+0x1231): undefined reference to `mcc_vint_size'
    /usr/bin/ld: unit_tests.c:(.text+0x124a): undefined reference to `mcc_vint_size'
    collect2: error: ld returned 1 exit status
    make[2]: *** [makefile:16: test.AppImage] Error 1
    make[1]: *** [makefile:2: makefile] Error 2
    make: *** [makefile:2: makefile] Error 2
    Compilation failed.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > libmcc_core.so: $(wildcard mcc/core/*.o)
    > gcc -lpthread -o $@ $^
    I doubt this is creating a valid .so file.

    Try using the 'file' utility.
    Code:
    $ file /usr/lib/openmpi/lib/libmpi.so.12.0.2 
    /usr/lib/openmpi/lib/libmpi.so.12.0.2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=cb7d48202b372be192a2ca3d3fcb534410182b75, stripped
    $ file a.out
    a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=58cb7d060501f3fb1f8a2d0434fcf151e7ec6ea1, not stripped
    >test.AppImage: test/unit_tests.o libmcc_core.so
    > gcc -o $@ $^
    Similarly, you don't just name the library, you need the -l syntax to refer to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Salem View Post
    > libmcc_core.so: $(wildcard mcc/core/*.o)
    > gcc -lpthread -o $@ $^
    I doubt this is creating a valid .so file.
    Did work because I had the objects but now swapped for this:
    Code:
    mcc_core_sources:=$(wildcard mcc/core/*.c)
    mcc_core_objects:=$(mcc_core_sources:%.c=%.o)
    mcc_test_sources:=test/unit_tests.c
    mcc_test_objects:=$(test_core_sources:%.c=%.o)
    Quote Originally Posted by Salem View Post
    Try using the 'file' utility.
    Code:
    $ file /usr/lib/openmpi/lib/libmpi.so.12.0.2 
    /usr/lib/openmpi/lib/libmpi.so.12.0.2: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=cb7d48202b372be192a2ca3d3fcb534410182b75, stripped
    $ file a.out
    a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/l, for GNU/Linux 2.6.32, BuildID[sha1]=58cb7d060501f3fb1f8a2d0434fcf151e7ec6ea1, not stripped
    Didn't bother since what you mentioned below fixed it
    Quote Originally Posted by Salem View Post
    >test.AppImage: test/unit_tests.o libmcc_core.so
    > gcc -o $@ $^
    Similarly, you don't just name the library, you need the -l syntax to refer to it.
    Now just getting this:
    Code:
    make test (in directory: ~/mitsy/src)
    gcc -lpthread -o libmcc_core.so
    /usr/bin/ld: /usr/lib/gcc/x86_64-pc-linux-gnu/9.2.0/../../../../lib/Scrt1.o: in function `_start':
    (.text+0x24): undefined reference to `main'
    collect2: error: ld returned 1 exit status
    make: *** [makefile:17: libmcc_core.so] Error 1
    Which is strange since unit_test.c consists of just this right now:
    
    Code:
    #include <mcc/core/tsc.h>
    #include <mcc/core/get.h>
    #include <mcc/core/base62.h>
    #include "core/vint.c"
    int main() {
    	int ret = test_vint();
    	return ( ret == EXIT_SUCCESS ) ? EXIT_SUCCESS : EXIT_FAILURE;
    }
    Compilation failed.

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Okay after some fiddling I realised the objects weren't being created/updated as I originally thought so I resolved that and got as far as this:
    Code:
    make test (in directory: ~/mitsy/src)
    clang -Wall -fPIC -I ../include -o test/unit_tests.o -c test/unit_tests.c
    clang -Wall -fPIC -I ../include -o mcc/core/mem.o -c mcc/core/mem.c
    clang -Wall -fPIC -I ../include -o mcc/core/vint.o -c mcc/core/vint.c
    clang -Wall -fPIC -I ../include -o mcc/core/base62.o -c mcc/core/base62.c
    clang -Wall -fPIC -I ../include -o mcc/core/vflt.o -c mcc/core/vflt.c
    clang -Wall -fPIC -I ../include -o mcc/core/tsc.o -c mcc/core/tsc.c
    clang -Wall -fPIC -I ../include -o mcc/core/get.o -c mcc/core/get.c
    gcc -shared -fPIC -L ../lib -lpthread -o ../lib/libmcc_core.so mcc/core/mem.o mcc/core/vint.o mcc/core/base62.o mcc/core/vflt.o mcc/core/tsc.o mcc/core/get.o
    gcc -fPIC -L ../lib -lmcc_core -o mcc_unit_test.AppImage test/unit_tests.o
    ./mcc_unit_test.AppImage
    make: execvp: ./mcc_unit_test.AppImage: Permission denied
    make: *** [makefile:22: test] Error 127
    Compilation failed.
    Anyone familiar with linux and forcing auto-mounted drives under current user ownership? I put my files on a fat32 drive cause I got tired of resetting ownership every time I had to reinstall my system when that poxy nvidia driver decided to black screen me at boot, I'm not linux savy enough to fix issues in purely the command interface so I just reinstall after running my live usb and backing up my data. I wouldn't mind using btrfs since reactos has a working driver for that which will no doubt get included in windows sooner or later (which is useful for compiling same code on windows to test mitsy there too) but I don't know how to make the contents publicly accessible to any user on any system I plug into.
    The most I know is "chmod -R 777 ./"

  5. #5
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Managed to sort the execution problem but in the process I lost the right to create and delete files, from what I can gather it is because the gid is no longer that of users and I can't find any info on how to get that gid or what that gid might be if fixed
    Edit: Fixed it, now I have a vague idea of what to do for setting a public directory
    For now in the disks utility I have this in the options:
    users,gid=985,umask=0,rw,exec,nodev,nofail,noauto, x-gvfs-show
    ]
    and I had to actually add myself to the users group so I'm thinking of setting the group for a btrfs drive to sys which I was a part of and is a simple 3, very likely to always be the same so unlikely to have to go through this crap again if I have to reinstall at any point. Weird how setting umask to 777 removed my permissions but clearing it gives me all the permissions I want
    Last edited by awsdert; 12-24-2019 at 02:24 PM.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Okay with my permission issues resolved I tried linkage again and it worked, the new problem now is when I try to run the app it can't find the library, here's my output:
    Code:
    make test (in directory: ~/mitsy/src)
    clang -Wall -fPIC -I ../include -o test/unit_tests.o -c test/unit_tests.c
    clang -Wall -fPIC -I ../include -o mcc/core/mem.o -c mcc/core/mem.c
    clang -Wall -fPIC -I ../include -o mcc/core/vint.o -c mcc/core/vint.c
    clang -Wall -fPIC -I ../include -o mcc/core/base62.o -c mcc/core/base62.c
    clang -Wall -fPIC -I ../include -o mcc/core/vflt.o -c mcc/core/vflt.c
    clang -Wall -fPIC -I ../include -o mcc/core/tsc.o -c mcc/core/tsc.c
    clang -Wall -fPIC -I ../include -o mcc/core/get.o -c mcc/core/get.c
    clang -shared -fPIC -L ../lib -lpthread -o ../lib/libmcc_core.so mcc/core/mem.o mcc/core/vint.o mcc/core/base62.o mcc/core/vflt.o mcc/core/tsc.o mcc/core/get.o
    clang -shared -fPIC -L ../lib -lpthread -o libmcc_core.so mcc/core/mem.o mcc/core/vint.o mcc/core/base62.o mcc/core/vflt.o mcc/core/tsc.o mcc/core/get.o
    clang -fPIC -L ../lib -lmcc_core -o mcc_unit_test.AppImage test/unit_tests.o
    ./mcc_unit_test.AppImage
    ./mcc_unit_test.AppImage: error while loading shared libraries: libmcc_core.so: cannot open shared object file: No such file or directory
    make: *** [makefile:24: test] Error 127
    Compilation failed.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You need to tell the dynamic link loader where to find your .so file.
    Shared Libraries
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by Salem View Post
    You need to tell the dynamic link loader where to find your .so file.
    Shared Libraries
    Thanks It helpfully had an example so I will make use of that after posting a new thread for a side project

    Edit: Noticed an oddity just as I was about to post and wound up resolving my problem before I could post, go figure 😋 (Tried to stick an emoji of side tongue sticking out, doesn't seem to be going well)
    Last edited by awsdert; 12-25-2019 at 10:08 AM.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by awsdert View Post
    Weird how setting umask to 777 removed my permissions but clearing it gives me all the permissions I want
    It's not weird at all. That's how umask works. From umask(2) - Linux manual page:
    Specifically, permissions in the umask are turned off from the mode argument to open(2) and mkdir(2).

  10. #10
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Quote Originally Posted by christop View Post
    It's not weird at all. That's how umask works. From umask(2) - Linux manual page:
    Ah that'll be why then, I just assumed that it would follow a normal user's common sense and set the permissions to the value given

    Any ways I finally fixed my linkage issues, the page salem linked wasn't clear enough in WHERE to use -Wl,-rpath or whether to stick a space in after the linker option or another comma, for anyone stuggling with the same problem here's the 3 key points you'll be looking for an example of:
    Code:
    $(mcc_core_lib): $(mcc_core_objects)
    	$(CC) -shared $(BFLAGS) -lpthread -o $@ $^
    
    $(mcc_test_app): $(mcc_test_objects) $(mcc_core_lib)
    	$(CC) $(BFLAGS) -lmcc_core -o $@ $(mcc_test_objects) -Wl,-rpath,$(LIB_PATHS)
    	
    %.o: %.c
    	$(CC) $(CFLAGS) -o $@ -c $<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linkage
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 12-18-2005, 03:56 PM
  2. Get sense of internal linkage and external linkage
    By gandalf_bar in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2003, 05:57 AM
  3. Regarding C Linkage
    By Archana in forum C Programming
    Replies: 4
    Last Post: 10-02-2002, 10:35 AM
  4. linkage with ada???
    By xds4lx in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2002, 09:03 AM
  5. Linkage
    By subnet_rx in forum C++ Programming
    Replies: 0
    Last Post: 01-01-2002, 07:50 PM

Tags for this Thread