• Create Account

# CMake linking issue with SFML/GLEW in Linux

11 replies to this topic

### #1hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 13 August 2013 - 07:04 PM

Okay, I decided I'd make a new post since the last one kinda got away from the original intent.  So, I'm trying to build my application and I've got all my code compiling, but it's not linking and I'm not sure why.  I kinda reworked the setup, I've got my engine that I've worked on and it compiles and links fine.  My game code is compiling, but it will not find the link to the GLEW and SFML libraries.  I cannot figure out why.  It works fine with my setup in Windows, but Linux apparently hates it.  Here's my top level CMakeLists.txt:

cmake_minimum_required(VERSION 2.6.4)
project(Balls)

include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/include${BATE_INCLUDES}
)

set(EXEC_NAME "Balls to the Walls")

set(Balls_SRCS
src/application.cpp
src/ball.cpp
src/ballswindow.cpp
src/brick.cpp
src/component.cpp
src/entity.cpp
src/main.cpp
src/position.cpp
src/system.cpp
src/velocity.cpp

)

set(Balls_INC
include/application.h
include/ball.h
include/ballswindow.h
include/brick.h
include/component.h
include/entity.h
include/position.h
include/system.h
include/velocity.h
)

add_executable(${EXEC_NAME}${Balls_SRCS} ${Balls_INC}) SET_TARGET_PROPERTIES(${EXEC_NAME}
PROPERTIES
COMPILE_DEFINITIONS
"GLEW_STATIC="
)

TARGET_LINK_LIBRARIES(${EXEC_NAME} BatE ) IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
# Linux specific code
SET_TARGET_PROPERTIES(${EXEC_NAME} PROPERTIES COMPILE_DEFINITIONS "IS_LINUX=" ) ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")

# Install target
install(TARGETS ${EXEC_NAME} DESTINATION bin)  BatE is my engine (Battle Engine). I've tried linking SFML here, but that didn't change anything. Any advice? ### #2hatfarm Members - Reputation: 224 Like 0Likes Like Posted 14 August 2013 - 08:40 PM Gah! I figured out why the problem is happening, a bit. Part of the reason is that the environment variables are not getting read properly by CMake. I've got SFML_ROOT defined in the /etc/environment file, and then I also can print out$SFML_ROOT in the terminal and it works, but CMake does not recognize them (I put a MESSAGE statement in there).  So, that's the problem, but I'm not sure how to fix it.  I even wrote a script that created the environment variable, then exported it and prints it out, it prints them all out, but it is not finding it inside of CMake...

Anyone have any ideas?

### #3hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 14 August 2013 - 09:43 PM

Well, I got around the issue a bit.  I created a bash script that exports the variables and then calls cmake-gui.  It works, it's lame, but it works.  However, then it has trouble finding any of the libraries that SFML links against.  This is incredibly frustrating.

### #4AllEightUp  Moderators   -  Reputation: 5403

Like
0Likes
Like

Posted 17 August 2013 - 06:24 PM

Sorry for the delay in replying.  Been running around lately..

In order to work with environment variables in CMake, you simply prepend "ENV" to the variable.  So, in order to tell Cmake to read your environment variable SFML_ROOT, you do the following:

SET( SFML_ROOT ENV{SFML_ROOT} )

Now SFML_ROOT has been imported from the environment variable and you can use it as a normal CMake variable.  That should get around the script file issue if I understood your needs correctly.

### #5hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 17 August 2013 - 08:19 PM

That's actually how I've been working with it.  I'm still not sure why that issue is happening, but now I'm having issues with the libraries linked by SFML.  I'm working on that, but I'm having an issue with CMake saying: "Cannot specify link libraries for target

"/usr/lib/i386-linux-gnu/libGLU.so" which is not built by this project."

Any ideas?  It appears I've got GLU installed, that doesn't appear to be the issue.  Why is it so much more brutal in Linux?

### #6AllEightUp  Moderators   -  Reputation: 5403

Like
0Likes
Like

Posted 18 August 2013 - 02:44 AM

That's actually how I've been working with it.  I'm still not sure why that issue is happening, but now I'm having issues with the libraries linked by SFML.  I'm working on that, but I'm having an issue with CMake saying: "Cannot specify link libraries for target

"/usr/lib/i386-linux-gnu/libGLU.so" which is not built by this project."

Any ideas?  It appears I've got GLU installed, that doesn't appear to be the issue.  Why is it so much more brutal in Linux?

This is the annoying thing in this thread (and the old one), I usually find Linux to be very simple to fix compared to all the f'd up Windows nonsense.  I don't mean to say the thread is annoying or anything, just that the worst case for me is always Windows and you always seem to run into Linux issues.

To be honest, it really looks like a problem in the CMake code but without further details I simply can not give you a lot of feedback.  I "THINK" that the error is specifying that you are trying to set target dependencies on something which is not part of your build, i.e. libGLU.  Unfortunately I don't see anything wrong with your posted CMake lists file but it doesn't have anything to do with libGLU so, that doesn't mean much.

Given the information I'd step back and remove that bash script and try to fix that bit first.  There is a problem somewhere and by using a script you are not fixing a possible source of the problems.  You should not need a script at all, that's the reason for CMake.

### #7hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 18 August 2013 - 09:27 PM

So, I've fixed a couple of issues.  This OpenGL issue has gone away, I was telling it to link against my executable and not the library, which was a problem, because the executable wasn't defined in the library's .txt file.  The script issue has just gone away, it wasn't solved by just restarting, because I didn't change anything between restarts in order to get it to work, but it's working without the script, so I'm not complaining about not making any headway.  I'm having a problem with it not finding X11 in Linux.  I have X11, because it compiled SFML fine, but as far as my makefile is concerned, it's not being linked properly.  I get this error when I try to make in Linux:

/usr/bin/ld: /home/hatfarm/SFML-2.1//lib/libsfml-window-s.a(WindowImplX11.cpp.o): undefined reference to symbol 'XGetWindowAttributes'
/usr/bin/ld: note: 'XGetWindowAttributes' is defined in DSO /usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libX11.so so try adding it to the linker command line
/usr/lib/gcc/i686-linux-gnu/4.7/../../../i386-linux-gnu/libX11.so: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status



Here's the code I have in my CMakeLists.txt file that should ensure that X11 is linked properly:

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux") find_package(X11 REQUIRED) set(LIB_INCLUDES${LIB_INCLUDES}
${X11_INCLUDE_DIR}) set(LIB_LINKS${LIB_LINKS} ${X11_LIBRARIES} ) ENDIF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")


That's in both my library and executable's CMakeFiles.txt...

I know that LIB_INCLUDES and LIB_LINKS are correctly used because it doesn't complain about OpenGL.  Any ideas?

### #8AllEightUp  Moderators   -  Reputation: 5403

Like
0Likes
Like

Posted 19 August 2013 - 02:18 PM

That's a pretty confusing one.  It looks as if the compiler "is" attempting to link the library, i.e. the "Invalid operation" message but the symbols are not getting read in correctly.  It makes me wonder if somehow you have mixed 32bit/64bit code somehow.  I setup a quick little experiment myself and had no problem getting SFML and X11 linked into a little test app, so that didn't give me any leads.  I'll keep thinking about it but I don't see anything particularly wrong.  The one thing I could suggest is to simply insert a "MESSAGE( "LIB_LINKS: \${LIB_LINKS}" )" after your block of code and make sure it looks correct, doesn't have any extra spaces, incorrect backslashes etc.  That's always my starting point, just start spitting out as much as possible till one looks incorrect.

### #9hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 19 August 2013 - 08:50 PM

Well, the weirdest thing is, I can make the examples for SFML fine.  There's specifically an X11 example, and I checked that CMake file in order to get what I should put here.  When I make that, it works fine.  I really can't figure out what the deal is here though...  I actually have the exact message print that you put, and nothing looks funny, except the include directories don't appear to have ';' after them, but that doesn't appear to be the issue.  This is incredibly frustrating, because I just want to code.

### #10AllEightUp  Moderators   -  Reputation: 5403

Like
0Likes
Like

Posted 21 August 2013 - 11:35 PM

Cross platform stuff is a pain in the bloody ass unfortunately. Cmake makes it considerably easier but is still not a silver bullet. If you can't get it figured out soon, you could give me source access and I'll fix things up for you. I'll even sign an NDA or whatever if needed. NOTE: This is a one time offer, just figure since you've used my new forum the most, it's worth it..

### #11hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 22 August 2013 - 09:27 AM

I REALLY REALLY appreciate the offer, it's in such an infant stage right now that I'm not that worried about an NDA or anything like that.  However, I kind of got it working last night.  I cleaned up the CMakeLists.txt and did things the way SFML does in their examples.  That got me past the immediate error, but moved it down the line to the pthread library.  SFML doesn't do a find_package(pthread) nor could I, so I decided that since Linux is easier to get these libraries with, I'd just use the dynamic libraries for Linux.  So, I have it use the static libraries for Windows and then in Linux it uses the static libraries.  I'm getting wildly different behavior in Linux (one of my assertions, which doesn't get thrown in Windows gets thrown immediately in Linux).  I'm going to have to work through that, but I'm happy to be doing that rather than messing with CMake.

Thank you so much for your help, I'm sure once I go to build on Mac, I'll have plenty more questions, but I'm doing okay now.

### #12hatfarm  Members   -  Reputation: 224

Like
0Likes
Like

Posted 22 August 2013 - 06:08 PM

I guess there is one more issue I'm having.  make clean is just not working.  When I type that in, it says:

"cmake_clean.cmake:21 (INCLUDE):

include called with wrong number of arguments.  Include only takes one
file."

This is in Linux, with MSVC2010, I have no issues.  Any ideas?

PARTNERS