mfontanini / cppkafka

Modern C++ Apache Kafka client library (wrapper for librdkafka)
BSD 2-Clause "Simplified" License
600 stars 206 forks source link

C2491 “cppkafka::BufferedProducer<BufferType,Allocator>::BufferedProducer” definition of dllimport function not allowed #206

Closed latermarch closed 2 years ago

latermarch commented 5 years ago

image win10 vs2015 I git clone the latest project from the master

latermarch commented 5 years ago

anyone helps

accelerated commented 5 years ago

Try defining cppkafka_EXPORTS. See header macros.h

latermarch commented 5 years ago

@accelerated thanks ,I tried, and delete the function in header buffered_producer.h image then it works well

steinio commented 5 years ago

Hi. I just got this very issue yesterday. Do I define cppkafka_EXPORTS in macros.h before the conditionals that uses it, and do I define this before building cppkafka.lib as well? Also, his solution of commenting out the method that causes the error, are there any cleaner ways around this problem?

accelerated commented 5 years ago

You would need to define it as a CMAKE build parameter such as -DCMAKE_CXX_FLAGS="<default ompile options> -Dcppkafka_EXPORTS". If you're on windows, the default suggested options are here. In visual studio you just put the -Dcppakfka_EXPORTS I would imagine in the same place as you would in a normal project built by VS compiler (Project's Properties -> C/C++ -> Preprocessor definitions).

I see the error explained here but the source code in the Buffered Producer seems OK to me. Anyway, try adding the definition above.

NOTE: IF you are trying to build the library, then cppkafka_EXPORTS has to be defined. If you are linking you application with the cppkafka.dll, then you MUST NOT define this since you are now importing the symbols.

steinio commented 5 years ago

This works for me btw, the fix you mentioned. I ran into issues when I tried to use the code that @latermarch commented out, bufferedproducer, which gave an "undecklared identifier for queue1" as well as queue2. I fixed this by simply removing the identifier as show below:

template <typename BufferType, typename Allocator> void BufferedProducer<BufferType, Allocator>::swap_queues(QueueType & queue1, QueueType & queue2, std::mutex & mutex) { std::lock_guard<std::mutex> lock(mutex); std::swap(queue1, queue2); }

I used the flags mentioned above, but my application still asked for the cppkafka.dll when trying to run, I might be misunderstanding something in the process here. I see that I used those flags, as well as using the CPPKAFKA_BUILD_SHARED=true flag, so I might have overwritten some things when building. I'll have to clean and redo it and check what works.

latermarch commented 5 years ago

image actually , I not only commented out the code ,and I put the function body in class where the function defines just like the picture above

jimtarrant commented 4 years ago

I too am getting errors like this building my code using Visual Studio 2019 and cppkafka version 0.3.1:

Error C2491 'cppkafka::BufferedProducer<BufferType,Allocator>::BufferedProducer': definition of dllimport function not allowed Error C2491 'cppkafka::CompactedTopicProcessor<Key,Value>::CompactedTopicProcessor': definition of dllimport function not allowed Error C2491 'cppkafka::BasicConsumerDispatcher<ConsumerType>::BasicConsumerDispatcher': definition of dllimport function not allowed

It seems that the member function definitions of these classes are not excluded when cppkafka_EXPORTS is not defined i.e. when consuming the headers in client code as opposed to building the shared library.

Attached is a patch to fix this issue. cppkafka_EXPORTS.zip

zhangyongzhe20 commented 4 years ago

ErrorC2491 'cppkafka::BufferedProducer<BufferType,Allocator>::BufferedProducer': definition of dllimport function not allowed. I also get the above error while I'm using VS 2015 and used vcpkg manager to install libs cppkafka:x86-windows (version: 0.3.1-2) and librdkafka:x86-windows (version: 1.2.0-2).

SpaceIm commented 2 years ago

Yes, it doesn't make sense to export a template declaration (or definition). It would make sense for an explicit instantiation.

Fix in 0.4.0:

--- a/include/cppkafka/utils/buffered_producer.h
+++ b/include/cppkafka/utils/buffered_producer.h
@@ -84,7 +84,7 @@ namespace cppkafka {
  */
 template <typename BufferType,
           typename Allocator = std::allocator<ConcreteMessageBuilder<BufferType>>>
-class CPPKAFKA_API BufferedProducer {
+class BufferedProducer {
 public:
     enum class FlushMethod {
         Sync,    ///< Empty the buffer and wait for acks from the broker.
--- a/include/cppkafka/utils/compacted_topic_processor.h
+++ b/include/cppkafka/utils/compacted_topic_processor.h
@@ -44,7 +44,7 @@ namespace cppkafka {
  * \brief Events generated by a CompactedTopicProcessor
  */
 template <typename Key, typename Value>
-class CPPKAFKA_API CompactedTopicEvent {
+class CompactedTopicEvent {
 public:
     /**
      * \brief Event type enum
@@ -111,7 +111,7 @@ private:
 };

 template <typename Key, typename Value>
-class CPPKAFKA_API CompactedTopicProcessor {
+class CompactedTopicProcessor {
 public:
     /**
      * The type of events generated by this processor
--- a/include/cppkafka/utils/consumer_dispatcher.h
+++ b/include/cppkafka/utils/consumer_dispatcher.h
@@ -70,7 +70,7 @@ namespace cppkafka {
  * * EOF: void(BasicConsumerDispatcher::EndOfFile, TopicPartition)
  */
 template <typename ConsumerType>
-class CPPKAFKA_API BasicConsumerDispatcher {
+class BasicConsumerDispatcher {
 public:
     /**
      * Tag to indicate a timeout occurred