Author Topic: Using a non-native C library in NASM  (Read 14884 times)

Offline Radiance

  • Jr. Member
  • *
  • Posts: 4
Using a non-native C library in NASM
« on: November 17, 2013, 12:01:11 PM »
Hi, I have a question.
How can I use external non-native C libraries (such as OpenCV) in NASM?

I have seem some examples, like calling printf/scanf from NASM using a 'extern' directive, but I need to call from a NASM program some functions which are not part of standard libraries but are contained in the 'cv.h' 'highgui.h' libraries of OpenCV.

Thanks, help is appreciated.

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Using a non-native C library in NASM
« Reply #1 on: November 17, 2013, 12:39:29 PM »
Hi!

Yes, you can, in NASM.
...
extern YourProcedure
...
call YourProcedure

You need to pass a library file to linker, so, it can find your procedure there.

x64 example, compile script:

nasm.exe -f win64 program.asm -o program.obj
golink.exe /entry main /console program.obj yourlibrary.dll

Quote
I have seem some examples, like calling printf/scanf from NASM using a 'extern' directive, but I need to call from a NASM program some functions which are not part of standard libraries but are contained in the 'cv.h' 'highgui.h' libraries of OpenCV.

printf/scanf, it doesn't matter,
you have to specify a library where these procedures ar located, always.
The same thing for your custom or other procedures,
you have to specify a library file, feed the linker,
where these, your procedures, are located.

Encryptor256!
« Last Edit: November 17, 2013, 12:50:46 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline Radiance

  • Jr. Member
  • *
  • Posts: 4
Re: Using a non-native C library in NASM
« Reply #2 on: November 17, 2013, 01:17:04 PM »
Thanks for your reply!
I am working in Ubuntu (so no .dll, just .h or .hpp), 32 bits (but I can virtualize 64)
Can you tell me how to link in this case?

Very appreciated again.
« Last Edit: November 17, 2013, 01:28:10 PM by Radiance »

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Using a non-native C library in NASM
« Reply #3 on: November 17, 2013, 01:38:50 PM »
Well, you can experiment...

This is how i did on windows with gcc as a linker.

"gcc.exe helloworld.o -o helloworld.exe -L"C:/MinGW/lib" -L"K:/win32/glfw" -lglfw3 -lgdi32  -lopengl32  -mwindows"

Maybe you can see some similarities for your linux. :D

"gcc helloworld.o -o helloworld -L"C:/YourLibPath" -lYourLibName"

Encryptor256!!!

Edit:
P.S. If you don't have libs, then, you need to build libs from source code files, like: .cpp, .h, other.

Quote
OpenCV is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android.

"interfaces" means, they have libs.

So, there should be libs.

Edit:
This might clarify even more, named: Shared libraries with GCC on Linux

Quote
gcc -L/home/username/foo -Wall -o test main.c -lfoo

Could be like this:

gcc -L/library/path/yourlibfolder  -o test main.o -lyourlibname

Bye!
« Last Edit: November 17, 2013, 02:28:24 PM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline Radiance

  • Jr. Member
  • *
  • Posts: 4
Re: Using a non-native C library in NASM
« Reply #4 on: November 17, 2013, 02:30:39 PM »
Thanks!
I didn't got it working exactly that way but the whole concept of linking got me searching until I got where I needed
I have another question, lets say I create a program called "find"

I got the linking working this way

nasm -f elf -o find.o find.asm
gcc -o find find.o mycfunctions.c
./find

it works nice, but lets say, I want to pass 2 strings as arguments/parameters when I execute the 'find' binary such as
./find str1 str2

Do you know how can I obtain/read these from within the NASM file?
Thanks!

Offline encryptor256

  • Full Member
  • **
  • Posts: 250
  • Country: lv
  • Win64 .
    • On Youtube: encryptor256
Re: Using a non-native C library in NASM
« Reply #5 on: November 17, 2013, 02:36:43 PM »
Arguments, it is possible.

I don't have direct answer, but you can look here, somebody has made a tutorial elsewere:

I don't have direct answer, but you can look here, Gunner has made a tutorial elsewere:

NASM - Linux Getting command line parameters

Bye!

Edit: Post updated with author name.
« Last Edit: November 18, 2013, 07:01:21 AM by encryptor256 »
Encryptor256's Investigation \ Research Department.

Offline Radiance

  • Jr. Member
  • *
  • Posts: 4
Re: Using a non-native C library in NASM
« Reply #6 on: November 17, 2013, 03:00:49 PM »
Thanks encryptor256!!! You've been a great helper

I hope to get enough knowledge in programming to help people as fast and good as you've done for now
It may have seen little, but at least now I don't feel like I'm standing nowhere.

Thanks!

Offline Gunner

  • Jr. Member
  • *
  • Posts: 74
  • Country: us
    • Gunners Software
Re: Using a non-native C library in NASM
« Reply #7 on: November 18, 2013, 01:56:47 AM »