1

Might be a duplicate but i wasn't able to find a answer for my question.

Usually if you want to import multiple functions from different c files in one main class, one would make a .h file and list up all functions from the .c sources.

My problem is, that all functions are wridden in .asm files. I used extern void asmFunc(int i, char c); to use the function in further code, but they become more and more and i don't want to end up with 200 of those extern lines at the beginning of my main. How can i create a library.h with all assembly functions so i can just write #include "library.h" at the beginning of my main class?

EDIT: I think i didn't give enough specific information:

  • I use MINGW gcc and NASM to compile c and asm files
  • I have to compile all files to .o first so i can match them
  • The first answer i got didn't work because my compile chain is a bit complicated thanks to the restrictions i have on Windows (i want Linux back)

It looks like this: I got a folder containing three folders with seperated library-like structures: bwt (basic window toolkit), io and std (stuff like strlen) They are compiled into bwt.o io.o and std.o.

Now i want to make a .h file for each of them so i can #include "bwt.h" in all kernel classes which need them. How do i get gcc to realize, that all functions in bwt.h are defined in bwt.o?

3
  • Have you tried putting all of the declarations in library.h? Commented Jun 24, 2020 at 12:33
  • Are you sure about the difference between a header file, and a library? Look at this answer. What you need to do is put all the declarations into a header file (library.h) which you can include in all files where it is used. Then, you need to compile a library (static: library.a or dynamic: library.so) which contains the code for the lib. Look at this answer. Commented Jun 24, 2020 at 12:46
  • Yes i am, usually i use .a for libraries but for this special case i only need the .o files since the code is not a usual application but a kernel so i can skip the step with .a I just want to get rid of the 142 extern lines and replace them with #include "alibstd.h" Commented Jun 24, 2020 at 12:50

2 Answers 2

2

Since you have a .o file, it doesn't matter that the source for those routines is assembly. As long as you know how to call them as C functions that's what matters.

So put all of your extern declarations for the assembly functions in library.h, then #include "library.h" in your main file. Then you can link them.

gcc -c main.c
gcc -o program main.o asmfunctions.o
Sign up to request clarification or add additional context in comments.

Comments

2

You can:

  1. Make a file
  2. Save the file as library.h (same folder as your C file)
  3. Put your extern declarations* in the file
  4. Add #include "library.h" in your C file

#include is literally copy-paste. You can put some code into another file, and then you can #include that file, and the compiler pretends you wrote the code in the main file directly. That's how it works.

* by the way, you don't need to write extern when declaring functions - only variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.