Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making conditions for linux and windows when linking libraries

Tags:

cmake

Windows VC++ 2008 linux gcc 4.4.3

I have the following problem. When I compile on windows I need the ws2_32 library. However, when I compile on linux, I don't need to link this.

My CMakeLists.txt

INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/client)
INCLUDE_DIRECTORIES($CLIENT_SERVER_SOURCE_DIR/cltsvr_ults)

# Link the library
LINK_DIRECTORIES($CLIENT_SERVER_DIR/client)

# Add the executable 
ADD_EXECUTABLE(clt test_clt)

# Link the executable to the client library
IF(WIN32)
    TARGET_LINK_LIBRARIES(clt client ws2_32)
ENDIF(WIN32)

IF(CMAKE_COMPILER_IS_GNUCXXX)
  TARGET_LINK_LIBRARIES(clt client)
ENDIF(CMAKE_COMPILER_IS_GNUCXXX)

I have tried unsuccessfully to compile under linux. Using the above conditions. However, It always tries to link the ws2_32 and I get a compile error. I think that the conditions aren't working, as it always falls through the WIN32 condition.

many thanks for any suggestions,

like image 538
ant2009 Avatar asked May 06 '10 09:05

ant2009


People also ask

How do shared libraries work on Linux?

Shared libraries are the most common way to manage dependencies on Linux systems. These shared resources are loaded into memory before the application starts, and when several processes require the same library, it will be loaded only once on the system. This feature saves on memory usage by the application.

Which of the following options is necessary to create a shared library?

The -shared or -dynamiclib option is required to create a shared library. The name of the source file is octagon.

What is the purpose of using shared libraries?

Shared Libraries are the libraries that can be linked to any program at run-time. They provide a means to use code that can be loaded anywhere in the memory. Once loaded, the shared library code can be used by any number of programs.


1 Answers

Since the WIN32 thing is such a fundamental part of CMake, I'd guess that there is more to this than what you mention.

Are you doing a clean check out of your code, or just copying up a whole directory on Linux? If you have all your CMake build files cached from the Windows build, maybe (just maybe!) something has snuck in there and "detects" itself as WIN32 on Linux?

Are you sure it is that line and not something else that causes the link to the stray Win-library? Maybe try a MESSAGE(STATUS "I am here")line within the IF(WIN32) just to make sure.

Are you sure the error is caused by linking that library? I can see a typo in your script, it should be IF(CMAKE_COMPILER_IS_GNUCXX) - you have an extra X on there. Perhaps you are not linking in what you thing you are, and that is why it fails.

like image 111
richq Avatar answered Sep 28 '22 19:09

richq