English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 16 January 2022, 11:17   #1
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
One .h file for multiple .c files?

Hello,

I guess this is more a question in relation to make. At the moment I have organized a program with a header file for each source file. So if I have common.c then I have common.h.

Now, it is not a must must, but I would like to have a single header file which exposed functions are implemented in multiple source files. In CMake I would achieve this through the add_executable command.

Code:
add_executable(App main.c hello.h hello1.c hello2.c)
(in the above example hello.h defines functions which some are implemented in hello1.c and others in hello2.c. main.c only needs to include hello.h to access all the functions.)

However I cannot see how to replicate using make.

Anyone has any advice?
Sim085 is offline  
Old 16 January 2022, 11:37   #2
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Code:
App: main.o hello1.o hello2.o
   gcc $^ -o $@

%.o: %.c
   gcc -c $< -o $@

main.o. hello.h

hello1.o: hello.h

hello2.o: hello.h
thomas is offline  
Old 16 January 2022, 13:54   #3
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
aaa... I need an object file for hello1 and hello2... thank you for the example, that was really helpful and managed to get it working like that.

by any chance is there a way how I can join multiple object files into a single object file?

Or I need to enter into library world for that?

Reason I am asking is to see if I can organize my compilation rather than dump everything when building the executable.

Quote:
Originally Posted by thomas View Post
Code:
 
App: main.o hello1.o hello2.o
   gcc $^ -o $@

%.o: %.c
   gcc -c $< -o $@

main.o. hello.h

hello1.o: hello.h

hello2.o: hello.h
Sim085 is offline  
Old 16 January 2022, 15:32   #4
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Quote:
Originally Posted by Sim085 View Post
aaa... I need an object file
It's not strictly needed to use object files, but it significantly decreases compile times. If you change only one source file, you need to recompile only that file and can then link the app from the existing object file plus the updated one.

If you don't use object files, you have to recompile the whole project for every change. This will take a lot of time, depending on the size of your project.


Quote:
by any chance is there a way how I can join multiple object files into a single object file?
Yes, you can combine several object files into one link library.

If you use an Amiga compiler like SAS, Dice or VBCC, then you can just concatenate all .o files into one .lib file using the AmigaDOS Join command.

If you use GCC, you use the ar command to create a .a file.

In either case you can just mention the .lib resp. .a file on the compiler command line to add all objects at once.

Quote:
Or I need to enter into library world for that?
You mean an AmigaOS shared library, a.k.a. .library file? That would probably be overkill.
thomas is offline  
Old 16 January 2022, 23:33   #5
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
Not sure what I am doing wrong?

I have updated my make file as follow;

Code:
all: App

%.o : %.c
    vc -c -o $@ $<
    
hello : hello1.o hello2.o
    join $^ hello.lib

App : hello main.o
    vc $^ hello.lib -o $@ $(LIBS)

main.o : hello.h main.h

hello1.o: hello.h

hello2.o: hello.h
The join command works fine when I run it from shell on its own, but when I run it through the make file I get an error "required argument missing" even though it is the exact same syntax which when run on its own works.
Attached Thumbnails
Click image for larger version

Name:	Screenshot 2022-01-16 at 23.32.27.png
Views:	62
Size:	13.6 KB
ID:	74441  
Sim085 is offline  
Old 17 January 2022, 09:02   #6
Exodous
Registered User
 
Join Date: Sep 2019
Location: Leicester / England
Posts: 201
If you put an "echo" command just before the "join", it will output the command it would run so you can see what arguments it is passing to join.
Exodous is offline  
Old 17 January 2022, 09:20   #7
thomas
Registered User
 
thomas's Avatar
 
Join Date: Jan 2002
Location: Germany
Posts: 6,985
Quote:
Originally Posted by Sim085 View Post
N
The join command works fine when I run it from shell on its own, but when I run it through the make file I get an error "required argument missing" even though it is the exact same syntax which when run on its own works.

Apparently your shell uses a different path with additional third-party commands. The original C:Join command requires a "to" or "as" keyword and does not output anything.
thomas is offline  
Old 17 January 2022, 09:46   #8
Sim085
Registered User
 
Join Date: Apr 2009
Location: N/A
Posts: 962
I was running make from ZSH. I have changed from join to C:join and it worked.



Many thanks for all the help. Now my make file looks more organized
... and learned some new stuff as well.



Quote:
Originally Posted by thomas View Post
Apparently your shell uses a different path with additional third-party commands. The original C:Join command requires a "to" or "as" keyword and does not output anything.
Sim085 is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Similar Threads
Thread Thread Starter Forum Replies Last Post
UAEUNP multiple files -jh- support.OtherUAE 3 22 February 2017 13:26
Multiple configuration files NRS support.WinUAE 10 17 August 2014 17:04
Moving multiple files/folders in 3.1??? britain4 support.Other 6 17 August 2014 02:59
Unpacking of Multiple .gz files andyhants support.Apps 4 17 March 2014 03:16
Copying multiple .adf's to one file Unregistered support.WinUAE 1 03 July 2001 12:01

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 00:56.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.09920 seconds with 14 queries