NASM - The Netwide Assembler

NASM Forum => Programming with NASM => Topic started by: Radiance on November 17, 2013, 12:01:11 PM

Title: Using a non-native C library in NASM
Post by: Radiance 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.
Title: Re: Using a non-native C library in NASM
Post by: encryptor256 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!
Title: Re: Using a non-native C library in NASM
Post by: Radiance 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.
Title: Re: Using a non-native C library in NASM
Post by: encryptor256 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 (http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html)

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!
Title: Re: Using a non-native C library in NASM
Post by: Radiance 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!
Title: Re: Using a non-native C library in NASM
Post by: encryptor256 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 (http://forum.nasm.us/index.php?action=profile;u=8646) has made a tutorial elsewere:

NASM - Linux Getting command line parameters (http://www.dreamincode.net/forums/topic/285550-nasm-linux-getting-command-line-parameters/)

Bye!

Edit: Post updated with author name.
Title: Re: Using a non-native C library in NASM
Post by: Radiance 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!
Title: Re: Using a non-native C library in NASM
Post by: Gunner on November 18, 2013, 01:56:47 AM
somebody has made a tutorial elsewere:
NASM - Linux Getting command line parameters (http://www.dreamincode.net/forums/topic/285550-nasm-linux-getting-command-line-parameters/)
Bye!

Yes, that would be me!