nats-io / nats.c

A C client for NATS
Apache License 2.0
384 stars 134 forks source link

How to compile and run a simple example like subscriber.c present in src code? #398

Closed adi-mohan closed 3 years ago

adi-mohan commented 3 years ago

I am new to NATS. I wanted to compile one of the given examples like subsciber.c on my system. NATS server is already running. I wanted to know how to compile a file like subscriber.c as stand-alone and the n run it. I was trying using gcc, but it's throwing linking error. I have already installed using cmake as given in Readme, with TLS off and streaming off as well.

Getting error like: /tmp/ccZHtHFk.o: In function printStats': subscriber.c:(.text+0x6c): undefined reference tonatsConnection_GetStats' subscriber.c:(.text+0x9b): undefined reference to natsStatistics_GetCounts' subscriber.c:(.text+0xda): undefined reference tonatsSubscription_GetStats' /tmp/ccZHtHFk.o: In function printPerf': subscriber.c:(.text+0x1d2): undefined reference tonats_Now' /tmp/ccZHtHFk.o: In function printUsageAndExit': subscriber.c:(.text+0x27e): undefined reference tonatsOptions_Destroy' subscriber.c:(.text+0x283): undefined reference to nats_Close' /tmp/ccZHtHFk.o: In functionparseUrls': subscriber.c:(.text+0x381): undefined reference to natsOptions_SetServers' /tmp/ccZHtHFk.o: In functionparseArgs': subscriber.c:(.text+0x3bd): undefined reference to natsOptions_Create' subscriber.c:(.text+0x507): undefined reference tonatsOptions_SetSecure' subscriber.c:(.text+0x585): undefined reference to natsOptions_LoadCATrustedCertificates' subscriber.c:(.text+0x6e3): undefined reference tonatsOptions_SetCiphers' subscriber.c:(.text+0x761): undefined reference to natsOptions_SetExpectedHostname' subscriber.c:(.text+0x7a5): undefined reference tonatsOptions_SkipServerVerification' subscriber.c:(.text+0xab9): undefined reference to natsOptions_UseGlobalMessageDelivery' subscriber.c:(.text+0xe10): undefined reference tonatsOptions_SetUserCredentialsFromFiles' subscriber.c:(.text+0xe99): undefined reference to natsOptions_SetWriteDeadline' subscriber.c:(.text+0xf31): undefined reference tonatsOptions_LoadCertificatesChain' subscriber.c:(.text+0xf6c): undefined reference to natsStatus_GetText' subscriber.c:(.text+0xf92): undefined reference tonats_PrintLastErrorStack' subscriber.c:(.text+0xfa1): undefined reference to natsOptions_Destroy' subscriber.c:(.text+0xfa6): undefined reference tonats_Close' /tmp/ccZHtHFk.o: In function onMsg': subscriber.c:(.text+0xfeb): undefined reference tonatsMsg_GetData' subscriber.c:(.text+0xffa): undefined reference to natsMsg_GetDataLength' subscriber.c:(.text+0x1008): undefined reference tonatsMsg_GetSubject' subscriber.c:(.text+0x1030): undefined reference to nats_Now' subscriber.c:(.text+0x105a): undefined reference tonats_Now' subscriber.c:(.text+0x1077): undefined reference to natsMsg_Destroy' /tmp/ccZHtHFk.o: In functionasyncCb': subscriber.c:(.text+0x10ac): undefined reference to natsStatus_GetText' subscriber.c:(.text+0x10d4): undefined reference tonatsSubscription_GetDropped' /tmp/ccZHtHFk.o: In function main': subscriber.c:(.text+0x116f): undefined reference tonatsOptions_SetErrorHandler' subscriber.c:(.text+0x118b): undefined reference to natsConnection_Connect' subscriber.c:(.text+0x11c1): undefined reference tonatsConnection_Subscribe' subscriber.c:(.text+0x11e0): undefined reference to natsConnection_SubscribeSync' subscriber.c:(.text+0x11ff): undefined reference tonatsSubscription_SetPendingLimits' subscriber.c:(.text+0x121f): undefined reference to natsSubscription_AutoUnsubscribe' subscriber.c:(.text+0x1234): undefined reference tonatsStatistics_Create' subscriber.c:(.text+0x1295): undefined reference to nats_Sleep' subscriber.c:(.text+0x12da): undefined reference tonatsSubscription_NextMsg' subscriber.c:(.text+0x12f9): undefined reference to nats_Now' subscriber.c:(.text+0x1305): undefined reference tonats_Now' subscriber.c:(.text+0x1332): undefined reference to nats_Now' subscriber.c:(.text+0x1342): undefined reference tonatsMsg_Destroy' subscriber.c:(.text+0x13a6): undefined reference to natsStatus_GetText' subscriber.c:(.text+0x13cc): undefined reference tonats_PrintLastErrorStack' subscriber.c:(.text+0x13d8): undefined reference to natsStatistics_Destroy' subscriber.c:(.text+0x13e4): undefined reference tonatsSubscription_Destroy' subscriber.c:(.text+0x13f0): undefined reference to natsConnection_Destroy' subscriber.c:(.text+0x13fc): undefined reference tonatsOptions_Destroy' subscriber.c:(.text+0x1401): undefined reference to `nats_Close' collect2: error: ld returned 1 exit status

kozlovic commented 3 years ago

I am not clear on this: have you installed CMake and you are able or unable to build with it? Does it work with CMake but you are asking how to build manually?

If you can compile with CMake, configure CMake to be verbose. From the build directory (assuming you have a directory 'build' in the main tree):

cmake .. -DCMAKE_VERBOSE_MAKEFILE=ON

Then make will print all the actual gcc commands invoked to build and link files.

kozlovic commented 3 years ago

As explained in the previous post, the original poster could use CMake to be able to compile, or at the very list, use the verbose mode to see what are the compile/link instructions to properly build the library or any of the examples. I am closing this issue now.